0%

Introduction to JavaScript II

  • if-else
  • switch case
  • for
  • for…in
  • for…of
  • while
  • do…while
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
// conditional statements
// if else
let hour = 8;
if (hour >= 6 && hour <= 12){
console.log("Good morning!");
}
else if (hour > 12 && hour <= 18){
console.log("Good afternoon!");
}
else
console.log("Good evening!");

// switch case
let role = 'guest';
switch (role){
case 'guest':
console.log('Guest User');
break;

case 'moderator':
console.log('Moderator User');
break;

default:
console.log('Unknown User');
}

/* // loops * 5
// for; while; do...while;for...in;for...of */
// for
console.log('for loop:')
for (let i = 0; i <= 5; i++){
if (i%2 !== 0) console.log(i)
}

// while
console.log('while loop:')
let i = 0;
while (i <= 5){
if (i%2 !==0) console.log(i);
i++;

}

// do-while
console.log('do-while loop:')
let j = 0;
do {
if (j % 2 !==0 ) console.log(j);
j++;
} while (j <= 5);

// words (condition){
// statement
// }

// for .. in: iterate all properties of object or all index of array elements.
const person1 = {
name: 'Joey',
age: 100
};
for (let key in person){
console.log(key,person[key]); // cannot use person.key
};

const colors = ['red','green','blue']
for (let index in colors){
console.log(index,colors[index]);
}

// for...of: iterate atoms of array
for (let color of colors)
console.log(color)

// break: end the whole loop
// continue: end current loop


Thanks for @Mosh‘s great video tutorials.

-------------End of blogThanks for your reading-------------