-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbooleanPower.js
More file actions
142 lines (120 loc) · 4.51 KB
/
booleanPower.js
File metadata and controls
142 lines (120 loc) · 4.51 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Run this file by using "node booleanPower"
*
* A boolean is a value that have logical meaning, commonly used for ifs
*/
// How to write it
let isRaining = false
let isSunny = true
// How to use it in if statement
if (isRaining) {
console.log('IsRaining if:', "Today is raining"); // "Today is raining"
} else {
console.log('IsRaining if:', "Today isn't raining");
}
// We can shorten the "if" statement by using "Conditional terniary operator"
let isHavePhone = true
let isListenMusic = isHavePhone ? "Let's hear music!" : "Nah, let's hear it later"
console.log("isListenMusic:", isListenMusic); // "Let's hear music!"
/**
* Boolean can also be written in boolean comparison operator
* means the return of this operator will be boolean
*
* Below opearor (==) is called "equal to" comparison operator
* Comparison operator will always return a boolean
*/
let isSame = 1 == 1
console.log('IsSame:', isSame); // true
/**
* Logical operator it's a way to compare two booleans
*
* Below operator (&&) is called "and" logical operator
*/
let isHaveDebt = true
let isHaveMoney = true
let isPaydebt = isHaveDebt && isHaveMoney
console.log('isPaydebt:', isPaydebt); // true
// More about boolean operators in https://www.w3schools.com/js/js_comparisons.asp
// A true boolean actually it just a 1 number
let boolean = true
let number = 1
console.log('Boolean plus number:', boolean + number) // 2
/**
* In Javascript, a false is commonly consists of five things
* (actually there's more that five, but these are the most common one)
* 1. false
* 2. empty string ('')
* 3. zero and negative numbers (0)
* 4. null
* 5. undefined
*
* They are called "falsy" values
*/
// empty string falsy
let emptyString = ''
if (emptyString) {
console.log("emptyString:", "If we fill the string, it will be true");
} else {
console.log("emptyString:", "Empty string is false"); // "Empty string is false"
}
// zero falsy
let zero = 0
if (zero) {
console.log("zero:", "If we increase the number, it will be true");
} else {
console.log("zero:", "If we decrease the number to zero and minus values, it will be false");
}
// null falsy
let nullVariable = null
if (nullVariable) {
console.log("nullVariable:", "If we change the null to another non falsy value, it will be true");
} else {
console.log("nullVariable:", "If we leave the value to be null, it will be false");
}
// undefined falsy
let undefinedVariable;
if (undefinedVariable) {
console.log("undefinedVariable:", "If we fill the variable with a non falsy value, it will be true");
} else {
console.log("undefinedVariable:", "If we leave the variable, not fill it with anything, it will be false");
}
// More about falsy values in https://developer.mozilla.org/en-US/docs/Glossary/Falsy
/**
* Logical operators and falsy values
*
* In Javascript, we often use logical / terniary opertor to decide what value
* should be inside a variable
*/
// Terniary operator with comparison operator
let userAge = 50
let ageMessage = userAge > 0 ? "Thank you!" : "Please input the right age number"
console.log("ageMessage:", ageMessage); // "Thank you!"
// Terniary operator with falsy operator
let userName = ""
let nameMessage = userName ? "Thank you!" : "Please input your name"
console.log("nameMessage:", nameMessage); // "Please input your name"
// Combo terniary operator
let isHaveBook = true
let isHaveTime = true
let whatToDo = isHaveBook ? isHaveTime ? "Go read" : "Read book later" : "Go buy a book"
console.log("whatToDo:", whatToDo); // "Go read"
let isStorm = false
let isWinter = false
let outsideCondition = isStorm ? "Don't go outside!" : isWinter ? "Get a coat" : "It's safe outside"
console.log("outsideCondition:", outsideCondition); // "It's safe outside"
// Logical (or) operator with truthy
let userParentAge = 55
let parentAgeMessage = userParentAge || "Please input the right age number"
console.log("parentAgeMessage:", parentAgeMessage); // 55
// Logical (or) operator with falsy
let userPetAge = 0
let petNameMessage = userPetAge || "Please input the right age number"
console.log("petNameMessage:", petNameMessage); // "Please input the right age number"
// Logical (and) operator with truthy
let userParentName = "William"
let parentNameMessage = userParentName && "Thank you for inputting name"
console.log("parentNameMessage:", parentNameMessage); // "Thank you for inputting username"
// Logical (and) operator with falsy
let userSiblingName = ""
let siblingNameMessage = userSiblingName && "Thank you for inputting name"
console.log("siblingNameMessage:", siblingNameMessage); // ""