-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13condition.js
More file actions
59 lines (47 loc) · 1.4 KB
/
13condition.js
File metadata and controls
59 lines (47 loc) · 1.4 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
// Conditional branching: if, '?'
// Sometimes, we need to perform different actions based on different conditions.
// To do that, we can use the if statement and the conditional operator ?, that’s also called a “question mark” operator.
// The “if” statement
// The if(...) statement evaluates a condition in parentheses and, if the result is true, executes a block of code.
let year = 2015;
if (year == 2015) console.log("You are right!");
if (year == 2015) {
console.log("That's correct!");
console.log("You're so smart!");
}
a = 1; //1 means true 0 means false
if (a) {
console.log("true");
a = 0;
}
// The if statement may contain an optional else block. It executes when the condition is falsy.
a = 0; //1 means true 0 means false
if (a) {
console.log("true");
a = 0;
} else {
console.log("false");
}
// Several conditions: “else if”
// Sometimes, we’d like to test several variants of a condition. The else if clause lets us do that.
// For example:
if (year < 2015) {
console.log("Too early...");
} else if (year > 2015) {
console.log("Too late");
} else {
console.log("Exactly!");
}
// ternary operator or Conditional operator ‘?’
let age = 19;
let accessAllowed = age > 18 ? true : false;
console.log(accessAllowed);
let message =
age < 3
? "Hi, baby!"
: age < 18
? "Hello!"
: age < 100
? "Greetings!"
: "What an unusual age!";
console.log(message);