-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_comparison_1.js
183 lines (154 loc) · 5.84 KB
/
js_comparison_1.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* eslint-disable use-isnan */
/* eslint-disable eqeqeq */
/* eslint-disable no-constant-condition */
/* falsy / nullish value : false, 0, '', null, undefined, NaN */
function FalsyComparison01() {
const falsy = [null, undefined, NaN, '', 0, false];
for (const i of falsy) {
if (i) {
console.log(`${i} :`, true);
} else {
console.log(`${i} :`, false);
}
}
console.log('' || 'hello');
}
function FalsyComparison02() {
console.log('"" ? Boolean("") == true :', '' ? true : false);
console.log('0 ? Boolean(0) == true :', 0 ? true : false);
console.log('NaN ? Boolean(NaN) == true :', NaN ? true : false);
console.log('null ? Boolean(null) == true :', null ? true : false);
console.log(
'undefined ? Boolean(undefined) == true :',
undefined ? true : false
);
}
function NotFalsyComparison02() {
console.log('"a" ? Boolean("a") == true :', 'a' ? true : false);
console.log('1 ? Boolean(1) == true :', 1 ? true : false);
console.log('-1 ? Boolean(-1) == true :', -1 ? true : false);
console.log('[] ? Boolean([]) == true :', [] ? true : false);
console.log('[1, 2] ? Boolean([]) == true :', [] ? true : false);
console.log('{} ? Boolean({}) == true :', {} ? true : false);
}
function NaNComparison() {
console.log('NaN == "" :', NaN == '');
console.log('NaN == "NaN" :', NaN == 'NaN');
console.log('NaN == 0 :', NaN == 0);
console.log('NaN == NaN :', NaN == NaN);
console.log('NaN == true :', NaN == true);
console.log('NaN == false :', NaN == false);
console.log('NaN == [] :', NaN == []);
}
function NullComparison() {
console.log('null == "" :', null == '');
console.log('null == "null" :', null == 'null');
console.log('null == 0 :', null == 0);
console.log('null == NaN :', null == NaN);
console.log('null == true ', null == true);
console.log('null == false ', null == false);
console.log('null == [] :', null == []);
console.log('null == null :', null === null);
}
function UndefinedComparison() {
console.log('undefined == "" :', undefined === '');
console.log('undefined == "undefined" :', undefined === 'undefined');
console.log('undefined == 0 :', undefined == 0);
console.log('undefined == NaN :', undefined == NaN);
console.log('undefined == true :', undefined == true);
console.log('undefined == false :', undefined == false);
console.log('undefined == [] :', undefined == []);
console.log('undefined == null :', undefined == null);
console.log('undefined == undefined :', undefined == undefined);
}
FalsyComparison01();
FalsyComparison02();
NotFalsyComparison02();
NaNComparison();
NullComparison();
UndefinedComparison();
/*
2 kinds of variable : let and const
: variable -> pointer -> memory address -> value
: we can change pointer for let variable (reassign)
: we can't change pointer for const variable (no reassign)
2 kinds of object :
: primitive object [ frozen | using value ]
: reference object [ not frozen | using pointer ]
@ comparison
# (x != true) and (x == false) : not-equivalent
# (x !== true) and (x === false) : not-equivalent
@ loose-comparison
# operand1-type and operand2-type / value1 and value2
# operand1-string and operand2-array / operand1-string and operand2-string / value1 and value2
# operand1-string and operand2-object / operand1-string and operand2-string / value1 and value2
# operand1-type1 and operand2-type2 / operand1-number and operand2-number / value1 and value2
@ strict-comparison
# operand1 and operand2 / [ type1 and type2 ] and [ value1 and value2 ]
@ conversion : string => number
# '' => 0
# 'number' => number
# non-numeric-and-non-empty string => NaN
@ conversion : string => boolean
# '' => false
# non-empty-string => true
@ conversion : number => string
# number => 'number'
@ conversion : number => boolean
# 0 => false
# NaN => false
# any-other-number => true
@ conversion : boolean => string
# true => 'true'
# false => 'false'
@ conversion : boolean => number
# true => 1
# false => 0
@ conversion : array => string
# [] => ''
# [e1] => 'e1'
# [e1, e2] => 'e1,e2'
@ conversion : array => number
# [] => 0
# [number] => number
# any-other-array => NaN
@ conversion : array => boolean
# any-array => true
@ conversion : object => string
# null => null
# undefined => undefined
# any-other-object => [object Object]
@ conversion : object => number
# null => 0
# any-other-object => NaN
@ conversion : object => boolean
# null => false
# undefined => false
# any-other-object => true
@ conversion : NaN, null, and undefined
# NaN isn't equivalent with anyone
# null isn't equivalent with anyone except null and undefined
# undefined isn't equivalent with anyone except null and undefined
@ conversion : condition checking
# !(x && y) and (!x || !y) are equivalent
# in a logical expression, every argument must be boolean
# if only x / !x is given in a condition checking, it is converted into following logical expression :
- x => Boolean(x) == true
- !x => !Boolean(x) == true
@ conversion : object in operation
# number1 + number2 / no-conversion => add
# number1 + boolean2 / number-conversion => add
# type1 + type2 / string-conversion => concat
# any-type-with-any-other-operator / number-conversion => any-other-operator
@ conversion : toString(), valueOf(), Symbol.toPrimitive(hint)
# every object has default toString() and valueOf() [ can be re-defined ]
# object can have a new method called Symbol.toPrimitive(hint) to override toString() and valueOf()
# toString() is invoked for alert
# re-defined valueOf() is invoked for every operation, comparison
# Symbol.toPrimitive(hint) is invoked for every operation, comparison, alert
# Symbol.toPrimitive(hint) returns on basis of hint and hint varies on basis of operation, comparison, alert
- add-operator : default
- any-other-operator : number
- comparison : default
- alert : string
*/