forEach() is a built-in array method that runs a callback function once for every element in an array. It's one of the most commonly used ways to loop over an array in modern JavaScript, and it reads more declaratively than a manual for loop — you're saying "do this for each item" rather than manually managing an index.

Basic forEach() Syntax

const fruits = ["apple", "banana", "cherry"];

fruits.forEach(function (fruit) {
  console.log(fruit);
});

// Same thing with an arrow function — the more common style today
fruits.forEach((fruit) => console.log(fruit));

Output:

apple
banana
cherry
apple
banana
cherry

Accessing the Index and the Full Array

The callback actually receives three arguments: the current element, its index, and the original array — useful when you need position information without managing a counter yourself.

const scores = [88, 92, 79];

scores.forEach((score, index) => {
  console.log(`Student ${index + 1}: ${score}`);
});

Output:

Student 1: 88
Student 2: 92
Student 3: 79

The Big Limitation: You Can't Break Out of It

This is the most important thing to know about forEach(): break and continue don't work inside it, and return only skips to the next callback invocation — it doesn't stop the loop.

const numbers = [4, 9, 15, 23, 8];

numbers.forEach((num) => {
  if (num > 20) {
    console.log(`Found: ${num}`);
    return; // only exits THIS callback call, the loop keeps going
  }
  console.log(`Checking: ${num}`);
});

Output:

Checking: 4
Checking: 9
Checking: 15
Found: 23
Checking: 8

Notice 8 still gets checked after the match on 23 — there's no way to stop early. If you need to exit as soon as a condition is met, use a regular for loop or for...of instead, both of which support break.

forEach() vs for...of

They're similar, but forEach() only works on arrays (and a few array-like structures), while for...of works on any iterable — strings, Maps, Sets — and supports break/continue. Use forEach() for a quick, readable "run this for every item" pass over an array when you know you won't need to stop early.