-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfor.js
63 lines (48 loc) · 1.12 KB
/
for.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
// question 1 for ,for in, for of and for each
let marketItems = {
morning: ["bread", "milk", "egg"],
lunch: ["bread", "milk", "egg"],
night: ["bread", "milk", "egg", "rice"],
};
/* output for
bread
milk
egg
rice
*/
// for loop to print food for night
for (let i = 0; i < marketItems.night.length; i++) {
console.log(marketItems.night[i]);
}
// for in
for (let key in marketItems) {
console.log(key, marketItems[key]);
}
/* output for in
morning [ 'bread', 'milk', 'egg' ]
lunch [ 'bread', 'milk', 'egg' ]
night [ 'bread', 'milk', 'egg', 'rice' ]
*/
let foodItems = ["bread", "milk", "egg", "rice"];
// for of
for (let index of foodItems) {
console.log(index);
}
/*
output for of
bread
milk
egg
rice
*/
// for each
let a = [1, 2, 3, 4];
a.forEach((num) => console.log(num));
/*
output foreach
1
2
3
4
*/
//----------------------------------------------------------------------------------------------------------------------------------