JavaScript For Loop
Loops run the same block of code several times. The classic for loop is the right tool when you know how many times to repeat, or when you need a counter — for example to walk through an array by index.
Syntax
for (initialization; condition; update) {
// body: runs while condition is true
}| Part | Runs | Typical code |
|---|---|---|
| initialization | once, before the loop starts | let i = 0 |
| condition | before every iteration; loop stops when false | i < 5 |
| update | after every iteration | i++ |
for (let i = 0; i < 5; i++) {
console.log("Iteration", i);
}Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Step by step: i starts at 0; 0 < 5 is true, so the body runs; i++ makes it 1; and so on. When i reaches 5 the condition fails and the loop ends.
Looping over an array
The counter makes a perfect index. Loop from 0 while i < array.length:
const planets = ["Mercury", "Venus", "Earth", "Mars"];
for (let i = 0; i < planets.length; i++) {
console.log(`${i + 1}. ${planets[i]}`);
}1. Mercury
2. Venus
3. Earth
4. Mars
Tip: If you only need the elements, not the index,
for…ofis simpler (see the For Of chapter). Use a classicforwhen you need the index, want to skip elements, or loop backwards.
Counting in different steps
The update expression can be anything — count by twos, count down, multiply:
let evens = "";
for (let i = 0; i <= 10; i += 2) evens += i + " ";
console.log(evens);
let countdown = "";
for (let i = 5; i > 0; i--) countdown += i + "… ";
console.log(countdown + "liftoff!");
const powers = [];
for (let n = 1; n <= 1000; n *= 10) powers.push(n);
console.log(powers);0 2 4 6 8 10
5… 4… 3… 2… 1… liftoff!
[ 1, 10, 100, 1000 ]
Accumulating results
A very common pattern: start with an initial value before the loop, update it inside:
const prices = [4.5, 12, 7.25, 3];
let total = 0;
let maxPrice = -Infinity;
for (let i = 0; i < prices.length; i++) {
total += prices[i];
if (prices[i] > maxPrice) maxPrice = prices[i];
}
console.log("Total:", total);
console.log("Average:", total / prices.length);
console.log("Most expensive:", maxPrice);Total: 26.75
Average: 6.6875
Most expensive: 12
Nested loops
A loop inside a loop runs the inner loop completely for each iteration of the outer one — useful for grids and tables:
for (let row = 1; row <= 4; row++) {
let line = "";
for (let col = 1; col <= 4; col++) {
line += String(row * col).padStart(4);
}
console.log(line);
} 1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Common mistakes
- Off-by-one:
i <= arr.lengthreads one element past the end (undefined). Use<. - Infinite loop: if the update never makes the condition false (e.g.
i--instead ofi++), the loop never ends and the page freezes. - Using
var: avarcounter is shared by all iterations and leaks out of the loop. Always uselet. - Changing the array while looping over it by index can skip or repeat elements.
All three parts of the header are optional — for (;;) is an endless loop that must be exited with break.
Test yourself
How many times does for (let i = 0; i < 3; i++) run its body?
- 2
- 3
- 4
- Infinite
When is the update expression (i++) executed?
- Before the first iteration
- After every iteration
- Before checking the condition the first time
- Only when the condition is false
Which condition correctly loops over every index of arr?
- i <= arr.length
- i < arr.length
- i < arr.length - 1
- i != arr