# Iterator.forEach()

> The `.forEach()` JS method of the `Iterator` object executes a function for each value of an iterator.

## How do I loop over an iterator with forEach?

The method that consumes the iterator, calling the callback once for each yielded value. Returns `undefined` and is used for side effects.

- Topic: Iterator
- Type: method
- Browser support: Chrome 122+, Chrome Android 122+, Edge 122+, Firefox 131+, Firefox Android 131+, Safari 18.4+, Safari iOS 18.4+

## Example

Wrong:

```js
users.values().forEach(async (user) => {
  await save(user);
});
console.log('All saved');
```

Right:

```js
for (const user of users.values()) {
  await save(user);
}
console.log('All saved');
```

`forEach()` does not wait for async callbacks; a `for...of` loop with `await` saves each user before logging.

## Common mistakes

- Chaining `.map()` after `forEach()` and getting a `TypeError`.

## Key points

- `forEach()` is the last call in a chain, because it returns `undefined`.
- Async work inside the callback is not expected to be awaited.

## Interview questions

### What is the difference between iterator `forEach()` and `for...of`?

Both read every value. `for...of` supports `break`, `continue` and `await`, while `forEach()` always runs to the end and ignores returned promises.

### Can I stop iterator `forEach()` early?

Only by throwing. Use `some()`, `find()` or a `for...of` loop with `break` when you need to stop.

## Related cards

- [Iterator.map()](https://wonderium.dev/js/iterator-map.md)
- [for...of](https://wonderium.dev/js/for-of.md)
- [Array.forEach()](https://wonderium.dev/js/array-for-each.md)
- [Iterator](https://wonderium.dev/js/iterator.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/iterator-for-each
- Last updated: 2026-11-25
