JavaScript Basics
Variables
Explicitly declared with var: var x=0, y=1, z=2;
Variables are untyped can change their value to different types
Unlike properties, cannot be deleted Undefined vs. unassigned
Automatic garbage collection of unreachable values
Circular references and memory leaks
Scopes
Global and local scopes
No code block scopes (e.g. inside if, for, while) window and global scope
Frames, multiple global scopes and security concerns
Scopes are delimited by functions
Scopes are chained Functions are invoked in the scope they were defined new Function() constructor scope Calling a function will add a call object (activation object) to the scope chain, having arguments as property
Scope Chains
Closures
Functions can be nested
Examples of closure:
function A() { var a = 'A scope'; function B() { var b = 'B scope'; alert(a); } B(); alert(b); } var uniqueID = (function() { var id = 0; return function() { return id++; }; })();
Using closure to create scope bind
Expressions and Operators
Expressions: literals and operators
Operators have precedence and associativity (unary, assign, ternary are right-to-left)
Arithmetic operators: +, -, *, /, %, ++, --
Equality and identity: == and ===, != and !==
Relational operators: >, <, >=, >=, in, instanceof String operators: +, >, <, >=, >= (alphabetical comparison)
Logical operators: &&, ||, !
Bitwise operators: &, |, ^, ~, <<, >>, >>> Assignment: =,
+=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^=
Other operators: ?:
typeof new delete void . , ()
Statements
Statements we already know: if, else, do/while, switch, for, for/in, break, continue with statement Empty statement ; switch also works with string values
Recap Scopes and Closures