Iterating Arrays: `for` vs. `forEach` and Beyond

Learn how to loop through JavaScript arrays using the classic `for` loop, the modern `forEach` method, and the powerful `for...of` loop. Understand their differences with interactive examples.

Lesson ProgressStep 1 of 9
[ "🍎" , "🍌" , "🍒" ]
...
0 EXP

Welcome! Let's explore how to visit every item in a JavaScript array, one by one.

// Our data: An array of fruits
const fruits = ["🍎", "🍌", "🍒"];

The `for` Loop

The traditional `for` loop gives you complete control over iteration. It consists of three parts separated by semicolons:

  1. Initialization: `let i = 0` - Runs once before the loop starts.
  2. Condition: `i > array.length` - Checked before every iteration. If `false`, the loop stops.
  3. Increment: `i++` - Runs after each iteration.
const items = ['a', 'b', 'c'];
for (let i = 0; i < items.length; i++) {
  console.log(items[i]);
}

Its main advantage is that you can use `break` to exit early or `continue` to skip an iteration.

System Check

What is the primary advantage of a `for` loop over `forEach`?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Loop Master

Construct a valid `for` loop to iterate an array.

🏗️
Iterator Expert

Correctly choose the loop type for a given scenario.

✍️
ForEach Pro

Prove your mastery of the `forEach` method syntax.

Mission: Build a `for` loop

Given the `colors` array, write a standard `for` loop that logs each color to the console. Our AI assistant will provide real-time feedback.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Match Loop to Feature

Drag the descriptions into the correct order to match the loops listed below.

1. `for` loop

2. `forEach` method

3. `for...of` loop

Cleaner, more readable syntax, but no `break`.
Full control, allows `break` and `continue`.
Clean syntax AND allows `break`.

Challenge: Complete the `forEach` Syntax

Fill in the missing parts to log each item in the `fruits` array.

const fruits = ["🍎", "🍌"];fruits.( () => {console.log();} );

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Array Iterator" project for feedback from other Net-Runners.

A Deep Dive Into JavaScript Array Iteration

Iteration—the act of processing each item in a collection—is one of the most fundamental tasks in programming. In JavaScript, arrays are the most common collection, and the language provides a rich, and sometimes confusing, set of tools to loop through them. Understanding the differences between these tools is crucial for writing clean, efficient, and bug-free code.

1. The Classic: The `for` Loop

This is the original, C-style loop that has been in JavaScript since the beginning. It's often called an **imperative** loop because you must *explicitly* tell it how to work: where to start, when to stop, and how to get to the next step.

const items = ['a', 'b', 'c'];

for (let i = 0; i < items.length; i++) {
  console.log(items[i]);
}
  • Pros:
    • **Full Control:** You have the index (`i`), the condition, and the increment.
    • **`break` and `continue`:** You can exit the loop early with `break` or skip an iteration with `continue`. This is its _ killer feature.
    • **Flexibility:** You can iterate backward (`i--`), skip items (`i += 2`), or even iterate over multiple arrays.
    • **Performance:** Technically the fastest loop, though the difference is rarely a concern.
  • Cons:
    • **Verbose:** It requires a lot of boilerplate (the `let i...` setup).
    • **Error-Prone:** Easy to make "off-by-one" errors (e.g., using `<= items.length`).
    • **Scope Issues:** Before `let` (with `var`), `i` would leak into the outer scope.

2. The Modern Standard: `forEach()`

The `forEach` method is a more **declarative** approach. You don't tell it *how* to loop; you just tell it *what to do* with each item by passing it a callback function.

const items = ['a', 'b', 'c'];

items.forEach(item => {
  console.log(item);
});

The callback function can actually take three arguments:

items.forEach((item, index, array) => {
  console.log(`Item ${index}: ${item}`);
});
  • Pros:
    • **Readability:** Very clean and easy to understand.
    • **Simplicity:** No manual index management.
    • **Scoping:** The callback function creates a new scope for each iteration, avoiding common closure-in-a-loop problems.
  • Cons:
    • **No `break` or `continue`:** This is the big one. You cannot stop a `forEach` loop early. It *will* run for every item.
    • **No Return Value:** `forEach` always returns `undefined`. It is purely for "side effects" (like logging or changing the DOM). It cannot be used to create a new array.

3. The Best of Both Worlds: `for...of` (ES6)

Introduced in ES6, the `for...of` loop gives you the clean, readable syntax of `forEach` but retains the powerful control flow of a `for` loop.

const items = ['a', 'b', 'c'];

for (const item of items) {
  if (item === 'b') {
    continue; // This works!
  }
  if (item === 'c') {
    break; // This also works!
  }
  console.log(item);
}
// Output: a
  • Pros:
    • **Clean Syntax:** The simplest syntax for getting just the values.
    • **`break` and `continue`:** Fully supported.
    • **Versatile:** Works on all "iterables" (Arrays, Strings, Maps, Sets, etc.).
  • Cons:
    • **No Index:** Does not give you the index directly. (Workaround: use `for (const [index, item] of items.entries())`).

4. The Trap: `for...in`

You will also see the `for...in` loop. **You should almost never use `for...in` to loop over an array.**

The `for...in` loop iterates over the **enumerable properties** (keys) of an object, not its values. For an array, these keys are the indices (`"0"`, `"1"`, `"2"`), but they are returned as **strings**, and the loop may also include properties from the array's prototype.

❌ Bad Practice (Do Not Use)

const items = ['a', 'b'];
Array.prototype.myProperty = 'oops';

for (const key in items) {
  console.log(key); 
}
// Output:
// "0"
// "1"
// "myProperty" <-- DANGER!

`for...in` is for objects, not arrays.

5. Beyond Iteration: Functional Methods

Often, you don't just want to *loop*; you want to *transform* data. In these cases, `forEach` is the wrong tool. You should use functional methods that **return a new array**.

  • `.map()` (Transforming): Creates a new array by applying a function to every item.
    const numbers = [1, 2, 3];
    const doubled = numbers.map(n => n * 2); 
    // doubled is [2, 4, 6]
  • `.filter()` (Selecting): Creates a new array with only the items that pass a test.
    const numbers = [1, 2, 3, 4];
    const evens = numbers.filter(n => n % 2 === 0); 
    // evens is [2, 4]
  • `.reduce()` (Aggregating): "Reduces" an array to a single value (like a sum or count).
    const numbers = [1, 2, 3];
    const sum = numbers.reduce((total, n) => total + n, 0); 
    // sum is 6
Key Takeaway:
  • Need to **`break`** or **`continue`**? Use `for` or `for...of`.
  • Need a **new array**? Use `.map()` or `.filter()`.
  • Need just a **simple, readable loop**? Use `forEach()` or `for...of`.
  • Need to loop over an **object**'s keys? Use `for...in`.

JavaScript Iteration Glossary

Iteration
The process of repeating a set of instructions for each item in a collection (like an array).
Index
The numerical position of an element in an array, starting from `0`.
`for` loop
An imperative loop with three parts: initialization, condition, and increment. Provides full control, including `break` and `continue`.
`forEach()`
An array method that executes a callback function for each element. It's declarative and readable but cannot be stopped early.
`for...of`
A modern loop that iterates over the **values** of an iterable (like an array or string). Supports `break` and `continue`.
`for...in`
A loop that iterates over the **enumerable property keys (indices)** of an object. Should not be used for arrays.
Callback Function
A function passed as an argument to another function, which is then executed. Used by `forEach()`, `map()`, `filter()`, etc.
`break`
A keyword that immediately terminates the execution of a loop (`for`, `for...of`, `while`).
`continue`
A keyword that skips the current iteration of a loop and proceeds to the next one.
`.map()`
An array method that **creates a new array** by applying a function to each element of the original array.
`.filter()`
An array method that **creates a new array** containing only the elements that pass a test (return `true`).
Side Effect
Any action a function takes that is observable outside the function itself, such as modifying a global variable, logging to the console, or changing the DOM. `forEach` is used for side effects.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching JavaScript and building robust web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest ECMAScript specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!