11. Loops#
Loops allow you to repeat a block of code multiple times. They are especially useful when working with arrays, processing data, or performing repetitive tasks. JavaScript offers several types of loops, each suited to different scenarios.
11.1. for Loop#
A for
loop repeats a block of code a specific number of times.
Syntax:
for (initialisation; condition; increment/decrement) {
// Code to run in each iteration
}
Explanation:
initialisation: Sets the starting point for the loop (e.g.,
let i = 0
).condition: Specifies when the loop should stop (e.g.,
i < 5
).increment/decrement: Updates the loop variable (e.g.,
i++
).
Note
The increment/decrement expression of a for
loop often uses in place increment ++
or decrement --
operators. These add or subtract 1
from the value of the operand, e.g. i
.
Examples
Print numbers from 1 to 5
for (let i = 1; i <= 5; i++) {
console.log(i);
}
1
2
3
4
5
Iterate through an array
let colors = ["red", "green", "blue"];
for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
red
green
blue
Count backward from 10 to 1:
for (let i = 10; i >= 1; i--) {
console.log(i);
}
10
9
...
2
1
11.2. while Loop#
The while loop repeats a block of code as long as a condition is true. Use it when the number of iterations isn’t fixed or known beforehand.
Syntax:
while (condition) {
// Code to run while condition is true
}
Examples
Print numbers from 1 to 5
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
1
2
3
4
5
Stop when a random number exceeds 0.8
while (true) {
let random = Math.random();
console.log(random);
if (random > 0.8) {
break; // Exit the loop
}
}
11.3. Looping Over Objects#
The for...of
loop is designed for iterating over iterable objects, such as arrays, strings, or sets. This style of
loop makes your life easier because it handles stepping through the elements for you.
Syntax:
for (element of iterable) {
// Code to run for each element
}
Examples
Iterate over an array
let fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
console.log(fruit);
}
apple
banana
cherry
Iterate over a string
let word = "hello";
for (let letter of word) {
console.log(letter);
}
h
e
l
l
o