-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path016-logical-operators.js
More file actions
105 lines (79 loc) · 2.14 KB
/
016-logical-operators.js
File metadata and controls
105 lines (79 loc) · 2.14 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
// 1: WHAT ARE LOGICAL OPERATORS?
// 2: THE AND OPERATOR (&&)
// let age = 25;
// let hasLicense = true;
// if (age >= 18 && hasLicense) {
// console.log("You can drive!");
// }
// let temperature = 28;
// let isSunny = true;
// if (temperature > 25 && isSunny) {
// console.log("Perfect day for the beach!");
// }
// 3: THE OR OPERATOR (||)
// let isWeekend = false;
// let isHoliday = true;
// if (isWeekend || isHoliday) {
// console.log("You can relax today!");
// }
// 4: THE NOT OPERATOR (!)
// let isLoggedIn = false;
// if (!isLoggedIn) {
// console.log("Please log in to continue");
// }
// let hasErrors = false;
// if (!hasErrors) {
// console.log("Form submitted successfully!");
// }
// let age = 15;
// if (!(age >= 18)) {
// console.log("You are not an adult");
// }
// 5: COMBINING LOGICAL OPERATORS
// let age = 20;
// let hasTicket = true;
// let hasID = false;
// if (age >= 18 && (hasTicket || hasID)) {
// console.log("Entry allowed");
// }
let score = 85;
let isBonus = false;
if (score >= 80 && !isBonus) {
console.log("You passed with a high score!");
}
// 6: TRUTHY AND FALSY VALUES
// **Falsy values** include:
// - `false`
// - `0`
// - `""` (empty string)
// - `null`
// - `undefined`
// - `NaN`
// let username = "John";
// if (username) {
// console.log("Welcome, " + username);
// }
let userColor = "";
let defaultColor = "blue";
let chosenColor = userColor || defaultColor;
console.log(chosenColor); // "blue"
// 7: SHORT-CIRCUIT EVALUATION
// With AND (&&):
// let isActive = false;
// let result = isActive && expensiveFunction();
// expensiveFunction() is never called!
// With OR (||):
// let hasAccess = true;
// let result = hasAccess || expensiveFunction();
// expensiveFunction() is never called!
// PRACTICAL EXAMPLE
// let username = "admin";
// let password = "1234";
// let isActive = true;
// if ((username === "admin" || username === "superuser") &&
// password.length >= 4 &&
// isActive) {
// console.log("Login successful!");
// } else {
// console.log("Login failed");
// }