Primitives and Variables
boolean value is either
false. It's used in conditional statements and logical operations. A
number is a value that conforms to the IEEE 754-2008 double-precision floating-point format (same as Java
double). It's used in arithmetic and bitwise operations. A
string is finite ordered sequence of zero or more UTF-16 characters (16-bit unsigned integers).
Null and Undefined
nothing, which are
undefined. Both of them are primitive data types. The
null keyword is a value of the same data type. It's used as a placeholder for a value that does not exist. The
undefined placeholder is the default value of a variable which has been declared but not defined. There exists a global variable named
undefined whose value is not defined.
Objects and Arrays
number. It could be any valid
string identifier. Arrays also have a
length property which is equal to one more than the greatest key of the array which is a
number. An array can be created using an array literal, which is a comma-separated list of values enclosed in square brackets. Like an object the values of an array can be accessed using either the array index notation or the dot notation.
Functions and Scopes
function followed by an optional function name, a comma-separated list of zero or more formal parameter names enclosed in parentheses and the body of the function enclosed in curly brackets.
Globals and Hoisting
Variables declared inside a function using the
var keyword. Such variables always belong to the global scope no matter where they are declared. This is known as a global leak. Local variables shadow global variables and variables in parent scopes with the same name. It's generally a bad idea to omit the
var keyword in variable declarations.
Like variables, functions may also be declared. A function literal is known as a
FunctionDeclaration when it's not used as an expression. They must have a function name. Otherwise it's known as a
undefined because variable definitions are not hoisted.
undefined. If it's greater than the number of formal parameters of a function then the extra arguments may still be accessible using the free variable
arguments in the function. The
arguments variable has a value which is an array-like structure which holds the values of all the arguments passed onto the function. It has a property named
length which holds the number of arguments the function was called with.
Every function has a free variable named
this which normally points to the global object. If a function is a method of an object then
this points to that object. If a function call is preceded by the keyword
this points to the newly created object. If the function does not explicitly return a value then the newly created object is returned.
prototype which points that the prototype of the instances of that function. The prototype object is shared by all the instances of a function and any property defined on the prototype object is inherited by them all. The prototype object has a property named
constructor which points back to constructor function.
When we create an instance of a function using the
null. If no match is found
undefined is returned.
Prototypes are useful. They allow the public methods of a constructor to be defined once and be shared amongst all the instances of the constructor. Any modification to the prototype object affects all the instances of the constructor. However to achieve inheritance the
prototype property of the derived constructor must point to an instance of the base constructor. This creates a prototypal chain. The instance of the derived constructor inherits all the properties of the instance of the base constructor and the prototype object of the base constructor. Since the instance of the base constructor doesn't have a property named
constructor which points back to the derived constructor we manually add it.
Closures and Higher-Order Functions
Scoping rules state that a variable can only be accessed from within its own scope or its children scopes. However closures allow indirect access to variables outside the scope they live in. A closure is a function which is defined in the lexical scope of a referencing environment. Thus it has access to all the variables declared within the context of this referencing environment. First-class functions can be passed as arguments to other functions, returned from other functions, and assigned to variables. A function becomes a closure when it's moved out of its referencing environment. When this happens the function "closes over" all the non-local variables accessed from within the function. These variables are known as upvalues and can then be accessed from outside their own scopes.
A higher-order function is simply a function which does at least take one or more functions as an argument or returns a function. Higher-order functions are usually passed closures as arguments or return closures themselves. All functional programming languages have higher-order functions. Closures and higher-order functions are powerful features which allow functions to maintain an internal state.