declaration
let vs var
If a variable is declared outside of a function or block it has the global
scope. Variables declared inside a function or as its parameters are scoped to
the function. The keywords let and const go one step further by scoping a
variable to a block of code (a loop or whatever). In pre-2015 javascript there
was no let keyword and instead the var keyword was used which does not
create block scoped variables.
multiple statements
let one = 1, two = 2;
In ES6 there's parallel assignment:
let [x, y, z] = [1, 2, 3];
swap values (without tmp)
You can use the above parallel assignment to swap values:
[x, y] = [y, x];
typeof
The typeof unary operator produces a string naming the type of the value you
give it.
console.log(typeof 4.5)
// number
console.log(typeof 'x')
// string
numbers
There is a single number type (64bit floating point). As a result 1 and 1.0
are considered equal.
Math.floor(number) can be used to "convert" a number to an integer.
order of operations
The order is determined by the precedence of the operators. The /, % and *
operators have the same precedence which is greater than that of + and -.
(1 - 2) + 1 = 1 - 2 + 1
and thus
100 + (4 * 11) = 100 + 4 * 11
NaN
The value NaN is the result of an operation that cannot produce a normal
result. It is not equal to any value including itself. You can detect if a value
is NaN with the isNaN(number) function.
boolean
console.log(2 < 3)
// false
console.log("Aardvark" < "Zoroaster")
// true
Uppercase values are always "less" than lowercase ones, to "Z" < "a". When comparing strings javascript goes over the characters left to right, comparing the unicode codepoints one by one.
In terms of precedence order the boolean operators are lower than numerical
operators, || has the lowest, then &&, >, == and so on.
1 + 1 == 2 && 10 * 10 > 50
false
null
undefined
'' (empty string)
0
NaN
All other values are true.
strings
A string literal can be wrapped in single or double qoutes. Backslash is the escape character. All characters are 16 bits wide (it was based on a 16bit unicode version before utf8). There is no character type. Strings also have a few built-in properties and methods, but are not themselves objects. You cannot assign new properties or methods.
properties
"seven".length = 5, strings have a length property. It's counting "runes" so
"Hello, δΈη!".length = 10.
Strings are immutable, but you can create a new string by concatenating other
strings with the + operator. Two strings with the same characters are considered
to be equal.
'c' + 'a' + 't' === 'cat'
methods
'cat'.toUpperCase() === 'CAT'
'la'.repeat(3) === 'lalala'
" okay\n ".trim() === "okay"
'coconut'.slice(4, 7) === 'nut'
String(6).padStart(3, "0") === "006"
'coconut'.indexOf("u") === 5
One difference between the array .indexOf is that the string version can
search for multi-character sub-strings.
"one two three".indexOf("ee") === 11
split and join
let sentence = "Secretarybirds specialize in stomping";
let words = sentence.split(" ");
console.log(words);
// ["Secretarybirds", "specialize", "in", "stomping"]
console.log(words.join(". "));
// Secretarybirds. specialize. in. stomping
escapes
\"
\'
\\
\/
\b = backspace
\f = formfeed
\n = newline
\r = carriage return
\t = tab
\u0041 = letter A
empty values
The values null and undefined are mostly interchangeable. The existence of
both is an accident in javascript's design.