-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoops.js
85 lines (58 loc) · 2.16 KB
/
Loops.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//* Loops - JavaScript
/* Loops (or cycles) in JavaScript are structures that allow several blocks of code to be executed
times. There are several types of loops in JavaScript, and each has its own application. Here are the
most common loops: */
//? 1. `for` loop:
// The `for` loop is used when you know the exact number of iterations.
for (let i = 0; i < 5; i++) {
console.log(i);
}
// In this example, the loop will run five times, displaying the numbers 0 to 4.
//? 2. `while` loop:
/* The `while` loop is used when you do not know how many times the loop will be executed, but the condition
must be true for it to continue executing. */
let counter = 0;
while (counter < 5) {
console.log(counter);
counter++;
}
// In this example, the `while` loop will also print the numbers 0 to 4.
//? 3. `do...while` loop:
/* Similar to the `while` loop, but ensures that the code block is executed at least once, even
if the condition is false. */
let count = 0;
do {
console.log(count);
count++;
} while (count < 5);
//? 4. `for...in` loop:
/* The `for...in` loop iterates over the enumerable properties of an object. It is especially useful for
iterate over the properties of an object. */
let person = { name: "John", age: 30, city: "Exampleville" };
for (let property in person) {
console.log(property + ": " + person[property]);
}
//? 5. `for...of` loop:
/* The `for...of` loop was introduced in EcmaScript 6 and is used to iterate over iterable elements
like arrays, strings, maps, sets, etc. */
let colors = ["red", "green", "blue"];
for (let color of colors) {
console.log(color);
}
//? Interrupt and Continue:
/* You can use the keywords `break` to exit a loop and `continue` to move to the next loop.
iteration. */
for (let i = 0; i < 5; i++) {
if (i === 3) {
break; // Exit the loop when i is 3
}
console.log(i);
}
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue; // Skip iteration when i is 2
}
console.log(i);
}
/* These are some examples of how you can use loops in JavaScript. Loops are essential for
repeat tasks and process data in programming. */