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.
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.
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.
console.log(fruits[0]), then console.log(fruits[1]), and so on for every item. Tedious and error-prone.The for...of Loop
The for...of loop gives you each value in the array directly. You never touch the index.
const colors = ["red", "green", "blue"]; for (const color of colors) { console.log(color); } // "red" // "green" // "blue"
Anatomy of for...of
The Index-Based for Loop
The classic for loop uses an index variable (usually i) that counts from 0 up to array.length - 1.
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...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
arr[i] vs arr[i+1]), loop backwards, skip every other item.Step-by-Step Approach
for...of: pick a descriptive variable name. For index: set start, condition, step.Example 1: Sum an Array
Calculate the total of all numbers in [10, 20, 30, 40].
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"].
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.
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
Common Mistakes
i <= arr.length instead of i < arr.length. This reads one past the end, giving undefined.for...in (iterates keys as strings) with for...of (iterates values). They do very different things.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?
for...of yields each value in the array directly. If you need the index, use an index-based for loop or entries().Question 2 of 5
const arr = [10, 20, 30];
for (let i = 0; i <= arr.length; i++) {
console.log(arr[i]);
}
What will the last value printed by this code be?
i <= arr.length allows i to reach 3, but arr[3] doesn't exist for a 3-element array. JavaScript returns undefined rather than throwing an error. The fix: use i < arr.length.Question 3 of 5
You want to print an array's elements in reverse order. Which loop is the best fit?
for...of always iterates front-to-back. The index loop lets you start at the last index and decrement with i--, making reverse iteration straightforward.Question 4 of 5
const words = ["hello", "world"];
for (const w of words) {
console.log(w.toUpperCase());
}
What is the output?
for...of assigns each value to w. Calling .toUpperCase() returns a new uppercased string. Using const is fine here because w is re-declared on each iteration, not reassigned.Question 5 of 5
const scores = [85, 92, 78, 95];
let max = scores[0];
for (let i = 1; i < scores.length; i++) {
if (scores[i] > max) {
max = scores[i];
}
}
console.log(max);
What value does max hold after the loop?
max. Whenever a score is larger, max updates. After checking all elements, 95 is the largest value found.