-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconditions.js
77 lines (64 loc) · 1.47 KB
/
conditions.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
/*
* conditions
****************/
// if/else
let m = 100; // 10
let p = 10;
if (true) {
console.log("passed the condition");
} else {
console.log("failed the condition");
}
// comparison
// == : equality of values
// === : equality of values and types
// != : difference of values
// !== : difference of values and types
// > : greater than
// < : less than
// >= : greater or equal to
// <= : less than or equal to
if (m === p) {
console.log("m in equal to p");
} else if (m > p) {
console.log("m is greater than p");
} else {
console.log("p is greater than m");
}
// logic operators
// OR
if (true || false) {
// this code can be executed only if one of the values is true
console.log("passed");
} else {
console.log("failed the condition");
}
// AND
if (true && false) {
// this code can be executed only if all the values are true
console.log("passed");
} else {
console.log("failed the condition");
}
// they can be mixed
if ((true || false) && (false || true)) {
console.log("success");
}
// switch cases
let a = 7;
switch (a) {
case 3:
console.log(3);
break;
case 7:
console.log(7);
break;
default:
console.log("the end");
}
// Special behavior of OR and AND operators
let n = true;
const b = n || "Julien"; //b = 'Julien' if n is a falthy value else = n
console.log("OR =>", `${b}`);
const hello = n && "Julien"; // hello = 'Julien if n is a truethy value else hello = n
console.log("AND =>", `${hello}`);