JavaScript cheat sheet
Table of Contents
Style
Indent: 2 space.
Naming
- Variable:
myVariablecamelCase. - Function:
myFunctioncamelCase. - Class:
MyClassPascalCase. - Closure:
makeMyClosureprefix “make”.
Type
- Number
- BigInt
- String
- Boolean
- Symbol
- null
- undefined
- Object
- Array
- Function
- Date
- RegExp
- Map
- Set
Operator
- Truthy, Falsy
- Falsy:
false,0,-0,"",null,undefined,NaN
- Falsy:
- 1
==“1”true - 1
===“1”false !=!==++--+=-=!!X: boolean(X)&&||!"hello" && 0 = 0"hello" && "world" = "world""hello" || null = "hello""hello" || "world" == "hello"Condition ? Expr1 : Expr2- If
ConditionistruethenExpr1, elseExpr2.
- If
A ?? B- If
Aisnullorundefined, thenB, elseA.
- If
...X[...myArr1, ...myArr2]- Unpacking(Array, String, Set, Map, Object)
Declaration
| |
String
| |
String Methods
Return new String
.toUpperCase().toLowerCase().trim().repeat(N).replace(X, Y).slice(Start, End).split(X)
Return Boolean, Number
.indexOf(X).includes(X).startsWith(X).endsWith(Str)
Array
| |
Array Methods
Edit original
.push(X).pop().unshift(X).shift().splice(Start, DeleteCount).sort(Callback).reverse()
Return new Array
.map(Callback).find(Callback).filter(Callback).reduce(Callback, InitialValue).concat(myArr2).join(Separator).slice(Start, End)
Return Boolean, Number, nothing
.indexOf(X).includes(X)- .length
.forEach((number, index) => {}) === for (i in arr){}
Object
| |
Object Methods
Return Array
.keys().values().entries()
Return Boolean
.hasOwnProperty(X)
Map
| |
Map Methods
Edit original
.set(Key, Value).delete(Key).clear()
Return Boolean, Value
.get(Key).has(Key).size
Set
| |
Set Methods
Edit original
.add(X).delete(X).clear()
Return Boolean, Value, nothing
.has(X).size.forEach()
Basic
| |
| |
| |
| |
| |
Function
| |
Class
| |
Other Technic
| |
Closure
| |
DOM
document
.getElementById().querySelector()// Tag.Class#Id.createElement()
const div = document.querySelector("div");
div.innerText = "hello";div.appendChild()
Event
.addEventListener(Event, Handler);
Import
In HTML
| |
ES6
| |
Node.js
| |