SKU // 23

JavaScript Loops

A loop runs the same block of code over and over, each time with a different value. Instead of writing ten nearly identical lines, you write the code once and tell JavaScript how many times to repeat it, or when to stop. Loops are what let a program handle 5 items or 5 million with the same few lines.

// Why Loops Exist

// Without a loop: repetitive and fragile
console.log("Row 1");
console.log("Row 2");
console.log("Row 3");
console.log("Row 4");
console.log("Row 5");

// With a loop: one line of work, repeated
for (let i = 1; i <= 5; i++) {
  console.log("Row " + i);
}
Row 1 Row 2 Row 3 Row 4 Row 5

Changing 5 to 500 in the loop version takes one keystroke. The manual version would need 495 more lines, and a typo in any one of them would be easy to miss.

// Every Loop Has Three Parts

  1. Start: where the loop begins, usually a counter set to 0 or 1
  2. Condition: checked before each pass; the loop keeps going while it is true
  3. Change: something that moves the loop toward its end, usually adding 1 to the counter

Each single pass through the loop body is called an iteration. The counter is traditionally named i (for "index" or "iteration"), but any name works.

// The Loop Types

Loop Best for Lesson
forA known number of repetitionsJS Loop for
whileRepeat until something changes (count unknown)JS Loop while
do...whileLike while, but always runs at least onceJS Loop while
for...ofEach value in an array or stringJS Loop for
for...inEach property name in an objectJS Loop for

Here are the three core loops side by side, each printing 0 to 2. Same job, different shapes:

// for: start, condition, and change all in one line
for (let i = 0; i < 3; i++) {
  console.log(i);
}

// while: start above, condition on top, change inside
let j = 0;
while (j < 3) {
  console.log(j);
  j++;
}

// do...while: body first, condition checked at the bottom
let k = 0;
do {
  console.log(k);
  k++;
} while (k < 3);

Each of the three loops prints exactly the same thing:

0 1 2

// Looping Over a List

The most common job for a loop is working through every item in a list. JavaScript stores lists in arrays. Arrays get a whole section later; here is all you need for now:

Arrays in 30 seconds
An array is a list of values in square brackets: const fruits = ["apple", "banana", "cherry"];. Each item has a numbered position called its index, starting at 0. fruits[0] is "apple", fruits[2] is "cherry". fruits.length is how many items there are: 3.
const fruits = ["apple", "banana", "cherry"];

for (let i = 0; i < fruits.length; i++) {
  console.log(i + ": " + fruits[i]);
}
0: apple 1: banana 2: cherry

The loop counter doubles as the index: on each pass, fruits[i] is the next item. Add a fourth fruit and the loop handles it automatically, because it stops at fruits.length, not a fixed number.

Watch out: off by one
Because indexes start at 0, the last item in a 3-item array is at index 2. So an array loop starts at i = 0 and runs while i < fruits.length. Write <= instead and the loop runs one extra time, reading fruits[3], which does not exist and gives undefined.

// Loop Scope

Where the counter is declared matters. Declared with let inside the loop header, it is a brand new variable that only exists inside the loop (block scope, from the JS Let lesson). Any outside variable with the same name is untouched:

let i = 5;
for (let i = 0; i < 10; i++) {
  // some code
}
console.log(i);    // the outer i
5

Leave off the let and the loop uses the existing outer i, changing it as it counts:

let i = 5;
for (i = 0; i < 10; i++) {
  // some code
}
console.log(i);    // the loop overwrote it
10

It ends at 10, not 9: the loop keeps adding 1 until the condition i < 10 fails, and that happens when i reaches 10. Always declare loop counters with let in the header, so each loop gets its own.

// The Infinite Loop

If the condition never becomes false, the loop never ends. The browser tab freezes and has to be closed.

// DANGER: i never changes, so i < 5 is always true
let i = 0;
while (i < 5) {
  console.log(i);
  // forgot i++
}
Watch out
Before running any loop, ask: "What makes this stop?" If you cannot point to the line that moves the loop toward its end, do not run it. If you do freeze a tab, close it; your code is still in your editor.

// Quick Check

How many times does this loop body run?

for (let i = 0; i < 4; i++) {
  console.log("hi");
}
Show answer
4 times, with i as 0, 1, 2, 3. When i becomes 4, i < 4 is false and the loop stops.

An array has 6 items. What is the index of the last one?

  1. 6
  2. 5
  3. 7
  4. -1
Show answer
B. Indexes start at 0, so 6 items use indexes 0 to 5. The last index is always length - 1.

Which loop is best when you don't know in advance how many passes you'll need?

  1. for
  2. while
  3. for...in
  4. None: you must always know the count
Show answer
B. while keeps going until a condition changes, like "until the player guesses right". for is best when you know the count up front.

// Try It

  1. Click Open Playground and run it. Change it to stop at 3, then at 20: only one number changes
  2. Make an array of three names and loop over it, printing each one with its index
  3. Change < to <= in your array loop and see the extra undefined
  4. Open both loop scope examples with their Try it Yourself buttons and confirm you get 5 and 10. Which one changed the outer i?
// Video Walkthroughs
Last updated: Oct 9, 2026, 11:56 PM ET