Skip to content

Instantly share code, notes, and snippets.

@zxdcm
Created June 3, 2019 21:00
Show Gist options
  • Save zxdcm/df15ab802d71e8df84bc5b804ccda386 to your computer and use it in GitHub Desktop.
Save zxdcm/df15ab802d71e8df84bc5b804ccda386 to your computer and use it in GitHub Desktop.
1. async vs defer
<script src="1.js" defer></script>
<script src="2.js" defer></script>
defer - preserve the execution order.
Naming:
jquery obj ? $name : name
variables in lowerCamelCase
6 data types (5 primary types):
1. number
2. string
3. boolean
4. null
5. undefined
6. object
binary + : cast to string if one of operands is string
binary -,/,%,* : cast to number
unary + : cast to number
Operators priority:
inc/dec => unary operators => binary operators => assignment
left associative.
If both operators have the same priority from left to right
assignment returns value;
=== strict comprasion (compare types and then values)
var a = 0 == false // true
var b = 0 === false // false
undefined == null // true
undefined cast to number = > Nan ; +undefined == Nan
null cast to number = > 0 ; +null == 0
When using > < >= <= on null | undefined - they cast to numbers.
When using == != on null | undefined : no cast. They are unique.
so null >= 0 => false because
Standars says that null or undefined == only to null or undefined.
0, "", null и undefined, NaN is false when used in if
z = 1 || 0 // 1 ( || returns first true)
z = 1 && 2 // 1 ( && returns last if all true or first false)
z = 0 && 1 // 0
Cast to number:
empty string => to zero
string : trim white spaces (\t, \n) => try to number => if fail => Nan;
Cast to bool:
undefined, null => false
any number (except zero) => true
zero => false
any string (except empty) => true
empty string => false
object => true
Functions
if/else, switch, for, while, do..while - do not affect on scope of variables.
hoisting
function without return ? undefined ;
// Function Declaration
function sum(a, b) {
return a + b;
}
// Function Expression
var sum = function(a, b) {
return a + b;
}
Function declared as Function declaration will be created before execution of the code.
var age = prompt('Сколько вам лет?');
var sayHi = (age >= 18) ?
function() { alert('Прошу Вас!'); } :
function() { alert('До 18 нельзя'); };
sayHi()
^ works with dunction expresion, but dont with declaration
Every single function call has own execution context;
function f () {
..
}
var g = f;
f = null;
g() // run time error
Solution: Named Function Expression
var f = function factorial(n) {
return n ? n*factorial(n-1) : 1;
};
var g = f; (g now has factorial function not f)
f = null;
alert( g(5) ); ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment