-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26_Conditionals.js
More file actions
127 lines (78 loc) · 2.26 KB
/
26_Conditionals.js
File metadata and controls
127 lines (78 loc) · 2.26 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
// ++++++++++++++ if - else statement +++++++++++++++
let age = 20
if (age > 34) { // only works when the condition inside the if statement is ture
console.log("your age is greater than 34")
}
else{
console.log("your age is smaller than 34")
}
// ++++++++++++++ implicite declaration ++++++++++++++++ // don't need {} and use ,
let balance = 500
if (balance > 400) console.log("rupees is greater then 400") ,
console.log("profit hua hai")
// ++++++++++++++ if - else ladder (if - else if ) +++++++++++++++++++
// if any block is executed than next block will not get executes.
const myBalance = 680
if (myBalance < 500) {
console.log("less than 500")
}
else if (myBalance < 600){
console.log("less than 600")
}
else if(myBalance < 700){
console.log("less than 700")
}
else {
console.log("less than 1000")
}
// Use of logic operators
const userLoggedIn = true
const debitCard = true
const panCard = false
// 1}
if (userLoggedIn && debitCard){ // and is used
console.log("valid user")
}
// 2}
if (userLoggedIn || panCard ){ // or is used
console.log("valid user")
}
// ++++++++++++++ Switch Statement ++++++++++
const month = 2
switch (month){
case 1 :
console.log("january")
break;
case 2 :
console.log("february")
break;
case 3 :
console.log("march")
break;
default :
console.log("not the starting three months of the year")
}
// if we dont apply break in switch statement then all the below statement will get printed except default
// NOTE
const cash = 250
switch (cash){
case (cash < 300) :
console.log("less then 300")
break;
case (cash < 400) :
console.log("less than 400")
break;
case (cash < 500) :
console.log("less than 500")
break;
case (cash < 600) :
console.log("less than 600")
break;
default :
console.log("good amount")
}
// NOTE : Use if - else - if for range or condition-based checks. as switch creates problem and works differently in JS.
// +++++++++++++++ Terniary Operator ++++++++++++++
// condition ? true : false
const num = 5
num > 2 ? console.log("you are good") : console.log("you are bad")