01 / 21

for...of & Index Loops

Master the two core patterns for iterating arrays in JavaScript: the modern for...of loop and the classic index-based for loop.

JavaScript

What Is an Array?

An array is an ordered list of values. Each value sits at a numbered position called an index, starting at 0.

"apple" "banana" "cherry" "date" index 0 index 1 index 2 index 3 const fruits = ["apple", "banana", "cherry", "date"]

Why Do We Need Loops?

Without a loop, processing each array element requires repetitive code. Loops let you write the logic once and run it for every element automatically.

✕ Without a Loop
You write console.log(fruits[0]), then console.log(fruits[1]), and so on for every item. Tedious and error-prone.
✓ With a Loop
You write the instruction once inside a loop. The loop visits each element and runs your code automatically.

The for...of Loop

The for...of loop gives you each value in the array directly. You never touch the index.

JS
const colors = ["red", "green", "blue"];

for (const color of colors) {
  console.log(color);
}
// "red"
// "green"
// "blue"

Anatomy of for...of

for ( const item of array ) { ... } keyword declaration (const or let) loop variable (holds each value) of iterable (any array/string) loop body (runs each time) No index needed Clean & readable

The Index-Based for Loop

The classic for loop uses an index variable (usually i) that counts from 0 up to array.length - 1.

JS
const colors = ["red", "green", "blue"];

for (let i = 0; i < colors.length; i++) {
  console.log(i, colors[i]);
}
// 0 "red"
// 1 "green"
// 2 "blue"

Anatomy of the Index Loop

for ( let i = 0 ; i < arr . length ; i ++ ) 1. Initialize Start counter at 0 2. Condition Stop before length 3. Update Move to next index i = 0 check: 0 < 3? ✓ run body i++ → 1 repeat until i = 3 3 < 3? ✕ → stop

for...of vs Index Loop

for...of Index-based for
Gives you Value directly Index (then you look up the value)
Needs index? No Yes, that is its purpose
Syntax Shorter, cleaner More verbose
Skip / reverse? Harder to control Easy: change start, step, or condition
Best for Simple iteration When you need the index or custom stepping

When to Use Which Loop

for...of
Print every element, sum all values, check if any element matches a condition.
Index loop
Compare neighbors (arr[i] vs arr[i+1]), loop backwards, skip every other item.
Either works
Build a new array from an old one, find the max/min, count occurrences. Pick whichever feels clearer to you.

Step-by-Step Approach

1
Ask: do I need the index? If yes, use the index-based for loop. If no, use for...of.
2
Write the loop header. For for...of: pick a descriptive variable name. For index: set start, condition, step.
3
Write the body. Use the loop variable (value or index) to perform your task.
4
Trace through mentally. Walk through the first 2-3 iterations to verify correctness.

Example 1: Sum an Array

Calculate the total of all numbers in [10, 20, 30, 40].

for...of
const nums = [10, 20, 30, 40];
let total = 0;

for (const num of nums) {
  total += num;
}

console.log(total); // 100

Example 2: Find an Index

Find the position of "cherry" in ["apple", "banana", "cherry", "date"].

index loop
const fruits = ["apple", "banana", "cherry", "date"];

for (let i = 0; i < fruits.length; i++) {
  if (fruits[i] === "cherry") {
    console.log("Found at index", i);
  }
}
// "Found at index 2"

Example 3: Reverse Iteration

Print [1, 2, 3, 4, 5] backwards: from last element to first.

index loop (reverse)
const nums = [1, 2, 3, 4, 5];

for (let i = nums.length - 1; i >= 0; i--) {
  console.log(nums[i]);
}
// 5, 4, 3, 2, 1

Visualizing the Flow

arr = ["a", "b", "c"] i = 0 i < 3 ? yes console.log(arr[i]) i++ no done

Common Mistakes

✕ Off-by-One Error
Using i <= arr.length instead of i < arr.length. This reads one past the end, giving undefined.
✕ Using for...in
Confusing for...in (iterates keys as strings) with for...of (iterates values). They do very different things.
✕ Forgetting i++
Omitting the update step creates an infinite loop: i stays 0 forever and the condition never becomes false.

Quick Reference Summary

Feature for...of Index for
Syntax for (const x of arr) for (let i=0; i<arr.length; i++)
Accesses Values Indices (then values via arr[i])
Reverse? ✕ Not directly ✓ Change start/step
Skip items? ✕ Not easily ✓ i += 2
Compare neighbors? ✕ No access to i+1 ✓ arr[i] vs arr[i+1]
Readability ★★★ ★★

Question 1 of 5

What does a for...of loop give you on each iteration?

Question 2 of 5

What will the last value printed by this code be?

Question 3 of 5

You want to print an array's elements in reverse order. Which loop is the best fit?

Question 4 of 5

What is the output?

Question 5 of 5

What value does max hold after the loop?