Grammar
Always use // for comments, even multi-line ones to avoid having to escape /*
characters.
Numbers
- There is a single, 64-bit floating point number type.
NaN
(Not-a-Number) is not equal to any value (including itself) and is essentially an illegal number value, but typeOf(NaN)===number is true- Use
isNaN(number)
to check for NaNs
Strings
- 16-bit character set and don't have character tpes.
- Backslashes (\) are used for escaping characters that could cause problems in strings.
- Strings are immutable.
Statements
- Inside a function, the var statement creates variables local to that function
- switch, while, for and do statements can have an optional label which can be used with
break
andcontinue
to provide more precise control over exactly which statement to break or continue. Format:labelname: statement
and thencontinue labelname;
- A block (statements with {}) do not create new scope - the variables are local to the function, not the block. So always define variables at the top of the function and not inside the blocks
- falsy values:
- false
- null
- undefined
- Empty string ' '
- The number 0
- The number NaN
- All other values are truthy including all objects & the string 'false'
- If no matches are found in
case
statements, the optional default statement is executed, otherwise the matching case statement is carried out - When using a for in loop, usually a good idea to use
hasOwnProperty(variable)
to make sure the property belongs to the object you want and is not instead an inherited property from the prototype chain:for (myvariable in object) { if (object.hasOwnProperty(myvariable)) { ... //statements to be executed } }
- A do while statement is always executed at least once as the while condition is only checked after the first iteration of the loop
catch
clause in a try statement must create a new variable that will catch the exception object- Scope of
thro
statement is thetry
block it's in, or thetry
of the function it's in - If there is no
return
statement,return===undefined
break
exits the statement andcontinue
forces a new iteration of the loop, both with the optional label mentioned above
Expressions
- For
expression ? expression2 : expression3
, if expression is truthy, execute expresion2; it it's falsy, execute expression3 - Invocation is
(expression1, expression2, etc)
- refinement is either
.name
or[expression]
as used in an array
Literals
- Names or strings used for specifying new objects object literals or arrays array literals
- Properties of the object are expressions and must be known at compile time
Functions
- A function literal defines a function value