-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.js
More file actions
95 lines (68 loc) · 1.9 KB
/
types.js
File metadata and controls
95 lines (68 loc) · 1.9 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
84
85
86
87
88
89
90
91
92
93
94
95
"use strict"
/*
Types in Javascript?
- Number
- Boolean
- String
- Null
- Undefined
- Object
*/
console.log(typeof(a));
var a = 100;
console.log(typeof(a));
a = 'Lee zii jia';
console.log(typeof a);
a = true;
console.log(typeof(a));
a = null;
console.log(typeof(a)); // Gotcha!
a = {}
console.log(typeof(a));
// Is null and undefined same?
/* Why javascript has both null and undefined
- You can assume it has like this, undefined is used by your program/Javascript to tell you the variable is not defined.
- Whereas null is used by programmer to initialise something as no value.
- Javascript never initializes a variable as null.
*/
// Compare only value
console.log(null == undefined); // Gotcha again!
/*
This is because the value that null and undefined hold is the same but not their types.
*/
// Compare both type and value
console.log(null === undefined);
/*
=== check both type and value and returns true only if both are matching.
== : JS tries to become smart here, take a example: 0 == "0"
- Left side is number and right side is string so it tries to coerce and then does the comparison.
https://dorey.github.io/JavaScript-Equality-Table/
*/
// Always use ===
/*
NaN
What's NaN?
Not a number.
*/
var a = 10 / "abc";
console.log(a);
// isNaN to check if number is not a number.
console.log(isNaN(a));
// case 1
a = "1";
console.log(isNaN(a));
// case 2
a = "A";
console.log(isNaN(a)); // What "A" is NaN?
// This is happening because of coercion, internally it happens like below:
// console.log(isNaN(Number("A")))
// case 3
console.log(NaN === NaN); // Gotcha!
// So how do we check if a variable is truly NaN
/*
Any number compare is itself is always true, except NaN, we use this to check if varable to truely a NaN.
*/
a = 1;
console.log(a !== a);
console.log(NaN !== NaN);
// SIDE NOTE: NaN == ? is always false