🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Array.forEach()

AI & DATA SCIENCE // for-each

forEach executes a callback for each array element. It always returns undefined and cannot be stopped (use for...of with break for that).

Syntax

array.forEach((element, index, array) => {
  // side effect
});

Deep Dive Course

**forEach** iterates over an array and runs a callback for each element. Unlike **map**, it does not return a new array — it's purely for **side effects** (logging, updating external state). It cannot be broken early (use `for...of` with `break` if early exit is needed).

1Understanding Array.forEach()

forEach iterates over an array and runs a callback for each element. Unlike map, it does not return a new array — it's purely for side effects (logging, updating external state). It cannot be broken early (use for...of with break if early exit is needed).

💡

If you need a return value, use map(). If you need early exit, use for...of. forEach is only for side effects.

editor.html
const items = ['pen', 'paper', 'eraser'];

// Side effect: logging
items.forEach((item, index) => {
  console.log(`${index + 1}. ${item}`);
});

// forEach returns undefined (not chainable)
const result = items.forEach(x => x.toUpperCase());
console.log(result); // undefined
localhost:3000

2Practical Example

Here is a real-world application of Array.forEach() showing how it is used in production JavaScript code.

editor.html
// Async forEach pitfall
const ids = [1, 2, 3];

// WRONG: forEach doesn't await
ids.forEach(async (id) => {
  await fetch('/api/' + id); // these run in parallel!
});

// CORRECT: use for...of
for (const id of ids) {
  await fetch('/api/' + id); // sequential
}
localhost:3000

3Best Practices

Follow these guidelines when working with Array.forEach():

1. Use forEach only for side effects, not transformations

2. Use for...of when you need break or continue

3. Avoid async forEach — use for...of with await instead

⚠️

Tip: If you need a return value, use map(). If you need early exit, use for...of. forEach is only for side effects.

editor.html
const items = ['pen', 'paper', 'eraser'];

// Side effect: logging
items.forEach((item, index) => {
  console.log(`${index + 1}. ${item}`);
});

// forEach returns undefined (not chainable)
const result = items.forEach(x => x.toUpperCase());
console.log(result); // undefined
localhost:3000

Examples

Example 01Basic Usage
const items = ['pen', 'paper', 'eraser'];

// Side effect: logging
items.forEach((item, index) => {
  console.log(`${index + 1}. ${item}`);
});

// forEach returns undefined (not chainable)
const result = items.forEach(x => x.toUpperCase());
console.log(result); // undefined
Example 02Advanced Example
// Async forEach pitfall
const ids = [1, 2, 3];

// WRONG: forEach doesn't await
ids.forEach(async (id) => {
  await fetch('/api/' + id); // these run in parallel!
});

// CORRECT: use for...of
for (const id of ids) {
  await fetch('/api/' + id); // sequential
}

Best Practices

  • Use forEach only for side effects, not transformations
  • Use for...of when you need break or continue
  • Avoid async forEach — use for...of with await instead

Interview Question

Why can't you use await inside forEach?

Hint: forEach doesn't handle async callbacks correctly.

forEach ignores the return value of its callback, including Promises. The async callback creates a Promise on each iteration, but forEach doesn't await it — all callbacks run immediately in parallel. Use for...of with await for sequential async operations, or Promise.all() for parallel.

Exercises

MediumPractice using Array.forEach() in a real scenario.
View Solution
const items = ['pen', 'paper', 'eraser'];

// Side effect: logging
items.forEach((item, index) => {
  console.log(`${index + 1}. ${item}`);
});

// forEach returns undefined (not chainable)
const result = items.forEach(x => x.toUpperCase());
console.log(result); // undefined

Frequently Asked Questions

Why can't you use await inside forEach?

forEach ignores the return value of its callback, including Promises. The async callback creates a Promise on each iteration, but forEach doesn't await it — all callbacks run immediately in parallel. Use for...of with await for sequential async operations, or Promise.all() for parallel.

Related Functions

mapfilterloopsfor