forked from aayushyadavz/JavaScript-Full-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathif_else.js
More file actions
81 lines (70 loc) · 2.19 KB
/
if_else.js
File metadata and controls
81 lines (70 loc) · 2.19 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
/* Control flow
In previous codes we have seen that our code is executing everywhere, but it should not
be excecuted always, whole code should run on conditional base and this is known as
Control or Logic Flow. */
/* Comparison Operators,
<, > : less than, greater than. eg. 1 < 2
<=, >= : less than, greater than equal to. eg. 2 <= 2
== : checks equal or not.
!= : check not equal to. eg. 3 != 2
=== : checks equal or not also with data types. eg. 2 === "2"
!== : check not equal to (strict check). */
// If
const isUserLoggedIn = true
if (isUserLoggedIn) {
console.log("You are logged in.");
} // Output: You are logged in.
// Else
const temprature = 53
if (temprature < 50) {
console.log("Less than 50");
} else {
console.log("Greater than 50");
} // Output: Greater than 50
// Block Scope
const score = 200
if (score === 200) {
let power = "fly"
}
// console.log(`Power: ${power}`);
/* Throws an error.
Short-Hand */
const balance = 1000
if (balance > 500) console.log("Balance Greater than 500");
// Nesting
if (balance < 500) {
console.log("less than 500");
} else if (balance < 750) {
console.log("less than 750");
} else {
console.log("less than 1200");
} // Output: less than 1200
// Logical Operators (Checks multiple conditions)
const userLoggedIn = true
const debitCard = true
const loggedInWithGoogle = false
const loggedInWithEmail = true
// && (AND)
if (userLoggedIn && debitCard /* && 2 == 2 */) /* left & right to && should be true */ {
console.log("Allow to shop.");
} // Output: Allow to shop.
// Note: No statement should be false.
// || (OR)
if (loggedInWithEmail || loggedInWithGoogle /* || true */) /* from both if anyone is true */ {
console.log("User Logged In");
} // Output: User Logged In
// Nullish coalescing operator (??): null, undefined
let val1;
val1 = 10 ?? 15
console.log(val1); // Output: 10
val1 = null ?? 10
console.log(val1); // Output: 10
val1 = undefined ?? 13
console.log(val1); // Output: 13
val1 = null ?? 10 ?? 15
console.log(val1); // Output: 10
// Ternary Operator (?)
// Condition ? true : false
const iceTeaPrice = 100
iceTeaPrice <= 100 ? /* true */ console.log("less than 80") : /* false */ console.log("more than 80")
// Output: more than 80