-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjscheatsheet.js
138 lines (110 loc) · 4.1 KB
/
jscheatsheet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Comment
/* Multi
line comment */
/**
* Documentation block
*/
// LITERALS
"hello" // literal string
2 // literal number
[1, 2, 3] // literal array
{a: 1, b: 2} // literal object
null // literal null
undefined // literal undefined
/hello/ // literal RegExp
// STRINGS
"hello" // double quotes string
'hello' // single quotes string
`hello` // template string
"'" // double quotes string containing single quote
'"' // single quotes string containing double quote
"" // empty string
"\"" // escaped double quote
'\'' // escaped single quote
"\n" // escaped new line
"\r" // escaped carriage return
"\r\n" // escape carriage return and new line
"\t" // escaped tab
let myName = "Tony";
"Name = " + myName // concatenation
`Name = ${myName}` // template string interpolation
// NUMBERS
2 // number
-2 // number with negative sign
2.5 // number with fraction
1_000_000 // number with digits separator
NaN // not a number
2 // denary notation
0b10 // binary notation
0o2 // octal notation
0x2 // hexadecimal notation
1e-2 // scientific notation
+ // add
- // subtract
* // multiply
/ // divide
% // modulo
// ARRAYS
[1, 2, 3] // number array
["a", "b", "c"] // string array
[1, "b", 3] // mixed array (avoid)
[[1, 2], [3, 4]] // nested array
let myArray = [1, 2, 3];
myArray[0] // get element at index
myArray[0] = 2; // set element at index
// OBJECTS
{a: 1, b: 2} // object
{"a": 1, "b c": 2} // object with quotes around keys to circumvent javascript name rules (eg no spaces)
{1: "a", 2: "b"} // object with number keys and string values
{} // empty object
{a: {b: 2}} // nested object
let x = "s";
{x: "a"} // literal key and value => {x: "a"}
{x: x} // symbolic value => {x: "s"}
{x} // implicit symbolic value => {x: "s"}
{[x]: "a"} // symbolic key => {s: "a"}
{[x]: x} // symbolic key and value => {s: "s"}
let obj = {a: 1, b: 2};
obj.a // get value with dot operator
obj["a"] // get value with square bracket operator
obj.a = 3; // set value with dot operator
obj["a"] = 3; // set value with square bracket operator
delete object['a']; // remove key-value pair with square bracket operator
delete object.a; // remove key-value pair with dot operator
// VARIABLES
var x = 2; // global/function variable declaration (usually avoid)
let x = 2; // scoped variable declaration
const x = 2; // scoped constant declaration
let x; // declaration without assignment
delete x; // delete declaration
let x = 2, y = 3; // multiple declarations
// destructed assignment from array
let arr = [1, 2]
let [a, b] = arr; // a = 1, b = 2
let [a] = arr; // pick only first item
let [, b] = arr; // skip first item
let [a, b, c=2] = arr; // default value
// destructed assignment from object
let obj = {a: 1, b: 2};
let {a, b} = obj; // a = 1, b = 2
let {a} = obj; // pick specific keys
let {a, c=2} = obj; // default value
// declaration with type comment
/** @type {number} */
let x = 2;
// FUNCTIONS
function f() {} // function
function f(x) {} // one parameter
function f(x=3) {} // parameter default value
function f(x, y) {} // two parameters
function f(...args) {} // variable number of parameters
function f([x, y]) {} // destructed array parameters
function f([x, y=3]) {} // destructed array parameters with default value
function f({x, y}) {} // destructed object parameters
function f({x, y=3}) {} // destructed object parameters with default value
function () {} // anonymous function (prefer arrow function)
() => {} // arrow function
x => {} // single parameter without parentheses
x => x + 1 // single expression return
x => ({}) // single expression return object
(() => {})() // immediately invoked function expression