-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-booleans.html
More file actions
121 lines (76 loc) · 2.17 KB
/
06-booleans.html
File metadata and controls
121 lines (76 loc) · 2.17 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Booleans</title>
</head>
<body>
<script>
/*
//the only booleans values
true
false
console.log(3 > 5 - 5);
console.log(5 === '5.00');
//if statements
if (false){
console.log("hello");
}
else {
console.log("else")
};
// See if someone is old enough to drive
const age = 15;
if (age >= 16) {
console.log('you can drive')
}
else if (age >= 14) {
console.log('almost there')
}
else {
console.log('you cant drive')
};
//AND operator
console.log(true && true);
console.log(0.2 >=0 && 0.2 < 1/3);
//OR operator
console.log(true || false);
//NOT operator
console.log(!true);
// truthy and falsy values.
// Falsy values (false, 0, '', undefined, null, NaN) any other value is truthy
if (5) {
console.log('truthy')
}
//
const cartQuantity = 1;
const cartQuantity2 = 0;
// since the cartQuantity is not 0 it'll behave like true truthy. if it was equal to 0 it'd behave like falsy
//so we using this as a shortcut to not type: cartQuantity > 0
if (cartQuantity) {
console.log('cart has products')
};
if (cartQuantity2) {
console.log('cart has products')
};
*/
//shortcuts for if-statements
//ternary operator ?
const result = 0 ? 'truthy' : 'falsy'
console.log(result)
//guard operator &&
//when first value is falsy, the operator will guard the next result and give the first one insteads
false && console.log('hello')
const message = 5 && 'hello'
console.log(message)
// Default operator (similar to guard but uses or " || ")
//the difference here is that when the first value is falsy, it will give the second one intead
let currency = 'EUR' || 'USD';
console.log (currency)
let currency2 = undefined ||'USD'
console.log(currency2)
//Shortcuts are not mandatory to use but they will save us some code
</script>
</body>
</html>