JavaScript
data type
- no char
- undefined
- nullish coalescing operator
??
give default value if the expression before isnullorundefined - optional chaining
?
skip method chain and returnundefinedif expression before isundefined
- nullish coalescing operator
assignment
- fallback
||execute if the former one fail
array […]
can contain multiple types of data
access data array1[index1]
.length.push().pop()work like stack.unshift().shift()work like queue.splice(index,number,…)removenumberitems fromindexand insert….reduce(fun1)take variable into function and put result back- spread (deep copy)
[...arr1] - rest
[attr1, ...arr2] = arr1
loop array
- the C way
for… ofloop.forEach(callback)
string
is just immutable array
formatted string
`blah ${var1}…`
.toUpperCase().toLowerCase().trim().slice(start,end)a.slice(1)return substring excludinga[0]a.slice(-1)returna[a.length - 1]
function
function fun1(var1,…) {
// …
}
anonymous function
(var1,…) => {
// …
}
minimum var1 => expression1
variadic function
argumentsmagic variable: array-like object- rest parameter
(arg0, …, ...args): array
comparison
- strong comparison
===!==does not convert type - weak comparison
==!=convert type
object
{
attr1: lit1,
// …
}
rather dictionary
deleteattribute.hasOwnProperty(attr)check if hasattr- rest
{attr1, ...obj2} = obj1 - spread
obj2 = {...obj1, attr1: val1}
quick initialization
{
attr1,
attr2,
// …
}
is equivalent to
{
attr1: attr1,
attr2: attr2,
// …
}
access attribute
.attr1object1[attr1]
immutable object Object.freeze(…)
method
defined inside of the object block
class class Class1{…}
constructor constructor(…){…}
use constructor new