Skip to content

Commit 1cd518a

Browse files
committed
Add conditionals examples in src/01-intro/02-conditionals.js
1 parent f3a91e6 commit 1cd518a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/01-intro/02-conditionals.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Path: src/01-intro/03-conditionals.js
2+
3+
/* Example 01 - if-else */
4+
let number = 0;
5+
if (number === 1) {
6+
console.log('number is equal to 1');
7+
} else {
8+
console.log('number is not equal to 1, the value of number is ' + number);
9+
}
10+
11+
/* Example 02 - ternary operator - if..else */
12+
if (number === 1) {
13+
number--;
14+
} else {
15+
number++;
16+
}
17+
18+
// is the same as
19+
number === 1 ? number-- : number++;
20+
21+
/* Example 03 - if-else-if-else... */
22+
let month = 5;
23+
if (month === 1) {
24+
console.log('January');
25+
} else if (month === 2) {
26+
console.log('February');
27+
} else if (month === 3) {
28+
console.log('March');
29+
} else {
30+
console.log('Month is not January, February or March');
31+
}
32+
33+
/* Example 05 - switch */
34+
switch (month) {
35+
case 1:
36+
console.log('January');
37+
break;
38+
case 2:
39+
console.log('February');
40+
break;
41+
case 3:
42+
console.log('March');
43+
break;
44+
default:
45+
console.log('Month is not January, February or March');
46+
}
47+
48+
// to see the output of this file use the command: node src/01-intro/03-conditionals.js

0 commit comments

Comments
 (0)