TExcelTAD
Home › JavaScript › JS For Loop

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

js
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++
js · try it yourself
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:

js · try it yourself
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…of is simpler (see the For Of chapter). Use a classic for when 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:

js · try it yourself
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:

js · try it yourself
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:

js · try it yourself
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.length reads one element past the end (undefined). Use <.
  • Infinite loop: if the update never makes the condition false (e.g. i-- instead of i++), the loop never ends and the page freezes.
  • Using var: a var counter is shared by all iterations and leaks out of the loop. Always use let.
  • 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?

  1. 2
  2. 3
  3. 4
  4. Infinite

When is the update expression (i++) executed?

  1. Before the first iteration
  2. After every iteration
  3. Before checking the condition the first time
  4. Only when the condition is false

Which condition correctly loops over every index of arr?

  1. i <= arr.length
  2. i < arr.length
  3. i < arr.length - 1
  4. i != arr