-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypeCoercion.js
More file actions
86 lines (24 loc) · 1.2 KB
/
typeCoercion.js
File metadata and controls
86 lines (24 loc) · 1.2 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
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
// console.log(2 == "2"); // true, because "2" is coerced to number 2
// console.log(2 === "2"); // false, because the types are different
// console.log("4" - 2); // 2, "4" is coerced to number 4
// console.log("4" + 2); // "42", the + operator treats one operand as a string and performs concatenation
// console.log(true + 1); // 2, true is coerced to 1
// console.log(false - 1); // -1, false is coerced to 0
// if (0) {
// console.log("This won't print because 0 is falsy");
// }
// if ("") {
// console.log("This won't print because empty string is falsy");
// }
// // Falsy values include: 0, "", null, undefined, NaN, and false.
// console.log(null == undefined); // true
// console.log(null === undefined); // false
// console.log(Number("123")); // ?
// console.log(String(123)); // ?
// console.log(Boolean(0)); // ?
// console.log(Boolean("hello")); // ?
// console.log([] + []); // ?
// console.log({} + []); // ?
// console.log([] + {}); // ?
// console.log("5" * "2"); // 10 because both strings are coerced to numbers
// console.log(Number("5") * Number("2")); // 10