-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_loops.js
More file actions
49 lines (38 loc) · 914 Bytes
/
02_loops.js
File metadata and controls
49 lines (38 loc) · 914 Bytes
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
// Example of a while loop in JavaScript
// Loops in JavaScript
let i = 1;
while (i <= 5) {
// console.log(`Iteration ${i}`);
i++;
}
// do...while loop example
let j = 1;
do {
// console.log(`Iteration ${j}`);
j++;
} while (j <= 5);
// for loop example
for (let i = 0; i <= 10; i++) {
// console.log('hello', i);
}
// for...in loop example (for iterating over object properties)
const person = {
name: "John",
age: 30,
city: "New York",
};
for (let key in person) {
console.log(`${key}: ${person[key]}`);
}
// for...in loop example (for iterating over array indices)
let arry = ["Ashish", "honey", "kevin", "codezen"];
for (let key in arry) {
console.log(key, arry[key]);
}
// for...of loop example (for iterating over iterable objects like arrays)
const fruits = ["Apple", "Banana", "Cherry"];
for (let fruit of fruits) {
console.log(fruit);
}
array.forEach(element => {
});