-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathifelse.js
More file actions
114 lines (80 loc) · 2.15 KB
/
Copy pathifelse.js
File metadata and controls
114 lines (80 loc) · 2.15 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
/* various ways to if else */
/* if */
if (condition) {
// if statements
}
/* if else */
if (boolean1) {
//statements 1
} else {
//statements else
}
/* if, else if, else */
if (boolean1) {
//statements 1
} else if (boolean2) {
//statements 2
} else if (booleanN) {
//statements N
} else {
// statetments else
}
/* if else using object */
const elseIfObject = {
"true": function () {
//statements 1
},
"false": function () {
//statements else
}
};
elseIfObject[boolean1]();
/* if, else if, else using object
useful pattern for constructing if else statement at runtime (dynamic if else)
can also be used for state machines*/
const ifElseIfObject = {
"case1": function () {
//statements for case 1
},
"case2": function () {
//statements for case 2
},
"case3": function () {
//statements for case 3 etc
}
};
ifElseIfObject[stringValue]();
/* if else using switch
here using return statement
can also use break, but things can get messy really fast when you
forget to break or, have for loops that use break inside ...
avoid switch if you can*/
const result = (function () {
switch (variable) {
case CASE1_VAR: { // use these to make block scoping possible
const x = y;
return x + 1;
}
case CASE2_VAR: {
const x = z;
return x + 2;
}
default: {
return default_thing;
}
}
}());
/* Used by minifiers, DO NOT WRITE MANUALLY */
/* if else using ? : operator
result becomes a if input is truethy and b otherwise
this can be abused by putting a function call instead of a and b effectively
making it a full if else statement equivalent */
const result = input ? a : b;
/* if else using || and && for assignement*/
const user = (users && users[0]) || null;
const variable = (condition && ifThing) || ElseThing;
/* if else using || and && with function*/
((boolean1 && functionIf()) || functionElse());
/* if else using || and && and function wrap*/
((boolean1 && (function () {/*statements1;*/ }())) ||/*else*/
(function () {/*statements2;*/ }()))