-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path04.1_ternary.js
82 lines (72 loc) · 2.79 KB
/
04.1_ternary.js
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
/**
* ========================================================
* Conditional (Ternary) Operator
* ========================================================
* The conditional (ternary) operator is a shorthand way to perform conditional (if-else-like) operations.
* The syntax is: condition ? expression1 : expression2
* If the condition is true, expression1 is executed, otherwise, expression2 is executed.
*/
/**
* ========================================================
* Basic Usage
* ========================================================
* In a simple example, you can use it to assign a value to a variable based on a condition.
*/
const isAdult = true;
const type = isAdult ? "Adult" : "Minor";
console.log(`Person is an: ${type}`); // Output: 'Person is an: Adult'
/**
* ========================================================
* Nested Ternary Operators
* ========================================================
* Ternary operators can be nested for multiple conditions, but this may reduce readability.
*/
const age = 25;
const ageGroup = age < 18 ? "Minor" : age < 60 ? "Adult" : "Senior";
console.log(`Age Group: ${ageGroup}`); // Output: 'Age Group: Adult'
/**
* ========================================================
* Using with Functions
* ========================================================
* You can also execute functions using the ternary operator.
*/
function greetMorning() {
return "Good Morning";
}
function greetEvening() {
return "Good Evening";
}
const isMorning = true;
console.log(isMorning ? greetMorning() : greetEvening()); // Output: 'Good Morning'
/**
* ========================================================
* As an Expression
* ========================================================
* The ternary operator is an expression, meaning it returns a value.
* You can use it inline with other operations.
*/
const score = 95;
console.log(`You ${score > 50 ? "passed" : "failed"} the exam.`); // Output: 'You passed the exam.'
/**
* ========================================================
* Nuances and Advanced Techniques
* ========================================================
*/
/**
* 1. Type Coercion
* ----------------
* Be cautious about type coercion when using the ternary operator, as it follows the same rules as other JavaScript operators.
*/
const value = "5";
const number = value == 5 ? "Loose equality" : "Strict inequality";
console.log(`Type Coercion: ${number}`); // Output: 'Loose equality'
/**
* 2. Avoid Side Effects
* ----------------------
* Avoid using ternary operators for operations that produce side effects, like assignments or function calls with side effects.
*/
/**
* 3. Readability
* --------------
* While chaining or nesting ternary operators can be powerful, it can also make code harder to read and maintain.
*/