-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionals.js
More file actions
142 lines (118 loc) · 3.38 KB
/
conditionals.js
File metadata and controls
142 lines (118 loc) · 3.38 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
/**
* CH. 01 - Essential Patterns
* Conditionals
*
* JavaScript Patterns - modern JS patterns with ES5 & ES6 examples
* @copyright 2016 - 2017, Ahad Bokhari
* license: MIT
*/
/* (i). Conditionals control behavior in JavaScript and determine whether or not pieces of code can run. They execute or skip other statements * depending on the value of a specified expressions.
*/
// a. The most basic conditional is the if / else statement. The statement has two different forms, the first being:
if (expression) {
// if the expression is truthy the statement is executed
...statement
}
// b. A single statement is required after the if keyword and parenthesized expression, however you can also use multiple statements within a
// block
if (!user) {
name = '';
address = '';
language = 'JavaScript';
}
// c. The second form executes if the expression is false and introduces an else clause to defer the control to
if (expression) {
...statement1
} else {
...statement2
}
// d. Follow the example below
if (n > 1) {
console.log('You have 1 new notification');
} else {
console.log('You have ' + n + ' new messages');
}
// e. Beware nested if statements with else clauses, use cauton that the else clause goes where it is appropriately needed
const i = 1;
const k = 2;
if (i == 1) {
if (k === 2) {
console.log('k equals 2');
// a good rule is the else clause is part of the nearest if statement
} else {
console.log('k does not equal 2');
}
}
/* The else / if statement statement is used when you need to execute many pieces of code. There is no else if in JavaScript,
* but it is a frequently used programming idiom
*/
// a. Familiarize yourself with the structure
if (expression) {
// ...execute code block #1
}
else if (expression) {
// ...execute code block #2
}
else if (expression) {
// ...execute code block #3
}
else {
// ...if all else fails, execute block #4
}
// b. else ...if without the placeholders, in the real world
if (quux === 1) {
// ...execute code block #1
} else if (quux === 2) {
// ...execute code block #2
} else if (quux === 3) {
// ...execute code block #3
} else {
// ...if all else fails, execute block #4
}
// c. the above else ..if idiom is much preferable and readble to this
if (quux === 1) {
// Execute code block #1
}
else {
if (quux === 2) {
// Execute code block #2
}
else {
if (quux === 3) {
// Execute code block #3
}
else {
// If all else fails, execute block #4
}
}
}
/* The switch statement simplifies both the appearance and the performance of multiple conditions. You can rewrite the previous example using a
* switch statement as follows:
*/
// a. Familiarize yourself with the structure
switch(quux) {
case 1: // Start here if foo == 1
// Execute code block #1.
break;
// Stop here
case 2: // Start here if foo == 2
// Execute code block #2.
break; // Stop here
case 3: // Start here if foo == 3
// Execute code block #3.
break; // Stop here
default: // If all else fails...
// Execute code block #4.
break; // stop here
}
// b. A simple example
function convert(x) {
switch(typeof x) {
case 'number': // Convert the number to a hexadecimal integer
return x.toString(16);
case 'string': // Return the string enclosed in quotes
return '"' + x + '"';
default: // Convert any other type in the usual way
return String(x);
}
}