-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeof.js
229 lines (167 loc) · 6.44 KB
/
typeof.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
console.log(typeof ""); // "string"
console.log(typeof "hello"); // "string"
console.log(typeof String("hello")); // "string"
console.log(typeof typeof 1); // "string"
console.log(typeof new String("hello")); // "object"
console.log(typeof 0); // "number"
console.log(typeof -0); // "number"
console.log(typeof 0xff); // "number"
console.log(typeof -3.142); // "number"
console.log(typeof Infinity); // "number"
console.log(typeof -Infinity); // "number"
console.log(typeof NaN); // "number"
console.log(typeof Number(53)); // "number"
console.log(typeof new Number(53)); // "object"
console.log(typeof true); // "boolean"
console.log(typeof false); // "boolean"
console.log(typeof new Boolean(true)); // "object"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object"
console.log(typeof Symbol()); // "symbol"
console.log(typeof []); // "object"
console.log(typeof Array(5)); // "object"
console.log(typeof arguments); // object
console.log(typeof argument); // undefined
console.log(typeof function () { }); // "function"
console.log(typeof new Function()); // "function"
console.log(typeof new Date()); // "object"
console.log(typeof /^(.+)$/); // "object"
console.log(typeof new RegExp("^(.+)$")); // "object"
console.log(typeof {}); // "object"
console.log(typeof new Object()); // "object"
console.log(undefined == null); // true
console.log(undefined === null); // false
console.log(isNaN(NaN)); // true
console.log(isNaN(null)); // false
console.log(isNaN(undefined)); // true
console.log(isNaN(Infinity)); // false
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN(null)); // false
console.log(Number.isNaN(undefined)); // false
console.log(Number.isNaN(Infinity)); // false
var x = NaN;
console.log(x == NaN); // false
console.log(x === NaN); // false
console.log(
// type(function () {
console.log("empty")
// })
); // "function"
// console.log(type(new Function())); // "function"
// console.log(type(class {})); // "function"
// console.log(type({})); // "object"
// console.log(type(new Object())); // "object"
// console.log(type(/^(.+)$/)); // "regexp"
// console.log(type(new RegExp("^(.+)$"))); // "regexp"
// console.log(type(new Date())); // "date"
// console.log(type(new Set())); // "set"
// console.log(type(new Map())); // "map"
// console.log(type(new WeakSet())); // "weakset"
// console.log(type(new WeakMap())); // "weakmap"
console.log(!!0); // false
console.log(!!-0); // false
console.log(!!""); // false
console.log(!!false); // false
console.log(!!NaN); // false
console.log(!!null); // false
console.log(!!undefined); // false
// TRUTHY VALUES
console.log(!!+Infinity); // true
console.log(!!-Infinity); // true
console.log(!![]); // true
console.log(!!{}); // true
console.log(!!new String("")); // true
console.log(!!new RegExp("")); // true
console.log(!!"Non-empty"); // true
console.log(100 + 50); // 150
console.log(100 - 50); // 50
console.log("100" + 50); // "10050"
console.log("100" - 50); // 50
console.log(null + 50); // 50
console.log(null - 50); // -50
console.log(void 0 + 50); // NaN
console.log(void 0 - 50); // NaN
console.log(true + 50); // 51
console.log(true - 50); // -49
console.log(false + 50); // 50
console.log(false - 50); // -50
console.log([] + 50); // "50"
console.log([] - 50); // -50
console.log([100] + 50); // "10050"
console.log([100] - 50); // 50
console.log({} + 50); // "[object Object]50"
console.log({} - 50); // NaN
console.log(new Date() + 1000); // "Thu May 31 2018 18:27:51 GMT+0100 (WAT)1000"
console.log(new Date() - 1000); // 1527787670595
console.log(+"100"); // 100
console.log(+null); // 0
console.log(+void 0); // NaN
console.log(+true); // 1
console.log(+false); // 0
console.log(+[]); // 0
console.log(+[100]); // 100
console.log(+[100, 50]); // NaN
console.log(+{}); // NaN
console.log(+new Date()); // 1527790306576
console.log(100 + ""); // "100"
console.log(null + ""); // "null"
console.log(void 0 + ""); // "undefined"
console.log(true + ""); // "true"
console.log(false + ""); // "false"
console.log([] + ""); // ""
console.log([100] + ""); // "100"
console.log([100, 50] + ""); // "100,50"
console.log({} + ""); // "[object Object]"
console.log(new Date() + ""); // "Thu May 31 2018 19:28:09 GMT+0100 (WAT)"
// Function that accepts a boolean as first argument
// and returns the boolean otherwise `false`
const booleanOrFalse = (value) => typeof value == "boolean" && value;
// Function that accepts a boolean as first argument
// and returns the boolean otherwise `true`
const booleanOrTrue = (value) => typeof value != "boolean" || value;
console.log(booleanOrFalse(true)); // true
console.log(booleanOrFalse(false)); // false
console.log(booleanOrFalse(1)); // false
console.log(booleanOrFalse(0)); // false
console.log(booleanOrFalse("String")); // false
console.log(booleanOrFalse({})); // false
console.log(booleanOrFalse([])); // false
console.log(booleanOrTrue(true)); // true
console.log(booleanOrTrue(false)); // false
console.log(booleanOrTrue(1)); // true
console.log(booleanOrTrue(0)); // true
console.log(booleanOrTrue("String")); // true
console.log(booleanOrTrue({})); // true
console.log(booleanOrTrue([])); // true
console.log(["a"] + ["b"]); // "ab"
console.log([] + []); // ""
console.log(![] + []); // "false", because ![] returns false while [] returns true.
// [] == ![] : Type coercion +[] == +![], +[] = 0 and ![] = false so it becomes +false => 0==0 => true
console.log(![]); // false
console.log(![]); // false
console.log([] == ![]); // true
console.log([] === ![]); // false
console.log(null == false); // false
console.log(null == true); // false
console.log(typeof null) // 'object'
console.log(null instanceof Object) // false
console.log(NaN instanceof Number) // false
console.log(typeof NaN) // 'number'
/* typeof new String("str") === typeof String("str") // false Why?
a. typeof new String(“str”) : ‘object’
b. typeof String(“str”) : ‘string’*/
console.log(typeof new String("str") === typeof String("str")); // false
// false value
Boolean('') // false
Boolean(0) // false
Boolean(-0) // false
Boolean(NaN) // false
Boolean(null) // false
Boolean(undefined) // false
Boolean(false) // false
// Truthy Values: Anything not in the list above is a truthy value like -
Boolean({}) // true
Boolean([]) // true
Boolean(Symbol()) // true
!!Symbol() // true
Boolean(function () { }) // true