-
Notifications
You must be signed in to change notification settings - Fork 0
/
7_types.js
62 lines (50 loc) · 1.35 KB
/
7_types.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
// primitive types :
// string, number, boolean, undefined, null, symbol
// everything else above - object type
let message = 'message';
console.log(typeof message); // output : string
console.log(typeof 12); // output : number
console.log(typeof true); // output : boolean
console.log(typeof globalThis); // output : object
console.log(Boolean('haha')); // output: true
// saatnya convert data type to another
// - eksplisit type conversion (kita yang ubah nyuruh JS coy) :
// - implisit type conversion( JS does it automatic)/ coercion :
console.log(typeof(String(12))); // number to string < eksplisit>
console.log('1' + 1); // coercion
console.log('1' * '9'); //coercion
console.log('1' * 9); //coercion
console.log(9 * '6'); //coercion
console.log(9 * true); // coercion
console.log(99 * false); //coercion
// falsy --> false, 0, '', null, undefined, NaN
if (true) {
console.log('truthy')
} else {
console.log('falsy')
}
// tips : jangan lakukan direct comparison di if statement
const userName = '';
if (!userName){
console.log('no user found');
}
// use triple equals === strict data type
// convert to real boolean if needed --> Boolean()
console.log('--- challenges ---');
/* chalenges 1:
- true
- false
- (true)
- false
- false
*/
/* challenges 2:
- false
- true
- false
- true
- true
*/
/* -- challenges 3:
' ', 0, undefined, NaN, null, false
*/