DocsHub
Loops

For Loop

Learn what loops are and how to use the for loop to repeat actions in JavaScript.

For Loop

Imagine you want to print numbers 1 to 100. You could write console.log(1), console.log(2) ... all the way to 100. That's 100 lines of the same thing. Nobody does that.

This is exactly the problem loops solve.

A loop is a way to repeat a block of code multiple times without writing it multiple times. You define what to repeat and how many times — JavaScript handles the rest.

for (let i = 1; i <= 100; i++) {
  console.log(i);
}

One block. Runs 100 times. That's the power of a loop.


The for Loop

The for loop is the most commonly used loop in JavaScript. You use it when you know exactly how many times you want to repeat something.

for (initialization; condition; update) {
  // code to repeat
}

It has three parts inside the parentheses, separated by semicolons:

PartWhat it doesExample
InitializationRuns once at the start — sets up a counterlet i = 0
ConditionChecked before every iteration — if false, loop stopsi < 5
UpdateRuns after every iteration — moves the counter forwardi++

How It Executes — Step by Step

true false Initializelet i = 0 Conditioni < 5 ? Run code block Update — i++ Exit loop

Let's trace exactly what happens with this loop:

for (let i = 0; i < 5; i++) {
  console.log(i);
}
Step 1 — Initialize:  i = 0
Step 2 — Check:       0 < 5 → true  → run block → prints 0 → i becomes 1
Step 3 — Check:       1 < 5 → true  → run block → prints 1 → i becomes 2
Step 4 — Check:       2 < 5 → true  → run block → prints 2 → i becomes 3
Step 5 — Check:       3 < 5 → true  → run block → prints 3 → i becomes 4
Step 6 — Check:       4 < 5 → true  → run block → prints 4 → i becomes 5
Step 7 — Check:       5 < 5 → false → loop exits

Output: 0 1 2 3 4

The initialization runs once. The condition and update run every iteration.


Basic Examples

Counting up

for (let i = 1; i <= 5; i++) {
  console.log(i);
}
// 1
// 2
// 3
// 4
// 5

Counting down

for (let i = 5; i >= 1; i--) {
  console.log(i);
}
// 5
// 4
// 3
// 2
// 1

Counting in steps

for (let i = 0; i <= 20; i += 5) {
  console.log(i);
}
// 0
// 5
// 10
// 15
// 20

You are not limited to i++. The update can be anything — i--, i += 2, i *= 2.


Looping Through an Array

This is where for loops become genuinely useful. Arrays store lists of data — loops let you go through every item.

const fruits = ["apple", "mango", "banana", "grape"];

for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}
// apple
// mango
// banana
// grape

fruits.length is 4 — so the loop runs for i = 0, 1, 2, 3 — which are exactly the valid indexes of the array.

Arrays in JavaScript are zero-indexed — the first item is at index 0, not 1. That's why we start with let i = 0 and use i < array.length instead of i <= array.length.

A real example — calculating a total:

const prices = [120, 450, 89, 300, 55];
let total = 0;

for (let i = 0; i < prices.length; i++) {
  total += prices[i];
}

console.log(`Total: ${total}`); // Total: 1014

Nested for Loops

You can put a for loop inside another for loop. The inner loop completes fully for every single iteration of the outer loop.

for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    console.log(`${i} x ${j} = ${i * j}`);
  }
}
// 1 x 1 = 1
// 1 x 2 = 2
// 1 x 3 = 3
// 2 x 1 = 2
// 2 x 2 = 4
// 2 x 3 = 6
// 3 x 1 = 3
// 3 x 2 = 6
// 3 x 3 = 9

For every value of i, j runs from 1 to 3. This is commonly used for grids, tables, and multi-dimensional data.


A Real Example — Finding the Highest Score

const scores = [72, 95, 88, 61, 99, 74];
let highest = 0;

for (let i = 0; i < scores.length; i++) {
  if (scores[i] > highest) {
    highest = scores[i];
  }
}

console.log(`Highest score: ${highest}`); // Highest score: 99

Loop through every score, keep updating highest whenever you find a bigger one. Simple, readable, effective.


Common Mistakes

Off-by-one error

const letters = ["a", "b", "c"];

// ❌ Wrong — i <= letters.length goes one index too far
for (let i = 0; i <= letters.length; i++) {
  console.log(letters[i]); // last iteration prints undefined
}

// ✅ Correct — i < letters.length
for (let i = 0; i < letters.length; i++) {
  console.log(letters[i]);
}

Infinite loop — forgetting the update

// ❌ Never do this — i never increases, condition is always true
for (let i = 0; i < 5;) {
  console.log(i); // runs forever, crashes the browser
}

Always make sure your update moves the counter toward making the condition false.


Summary

  • A loop repeats a block of code so you don't have to write it multiple times
  • The for loop is best when you know exactly how many times to repeat
  • It has three parts — initialization, condition, and update
  • Initialization runs once, condition is checked every iteration, update runs after every iteration
  • When the condition becomes false, the loop exits
  • Use i < array.length to loop through arrays — arrays start at index 0
  • Always make sure the loop has an exit condition — otherwise it runs forever

On this page