-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnullish-coalescing.js
More file actions
53 lines (31 loc) · 1.01 KB
/
Copy pathnullish-coalescing.js
File metadata and controls
53 lines (31 loc) · 1.01 KB
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
//: Nullish coalescing (??)
/// return its right-hand when its left-hand side operand is null or undefined and left-hand is not undefined or null when return left-hand side.
const result = undefined ?? 'Hello';
console.log(result) // Hello
const result1 = 123 ?? 'Hello'
console.log(result1) // 123
// because left-hand side not null or undifined
const result2 = NaN ?? 'Hello';
console.log(result2) // NaN
const result3 = true ?? 'Hello'
console.log(result3) // true
const result4 = false ?? 'Hello'
console.log(result4) // false
const result5 = 'Hello World' ?? 'Hello';
console.log(result5)
const result6 = 3 > 5 ?? true;
console.log(result6) // false
// false because 3 > 5 evakuates to false
const result7 = [1,2,3,4,'Hello'] ?? 'Hello World';
console.log(result7)
/// second example
// old method
const addUser = (user,id) => {
user.id = id !== null || id !== undefined ? id : 'Unknown'
return user;
}
// new method
const addUser1 = (user,id) => {
user.id = id ?? 'Unknown';
return user;
}