Iterator.prototype.forEach()
Baseline Newly available
This feature works across the latest devices and browser versions. This feature might not work in older devices or browser versions. It's been available across browsers since March 2025.
The forEach() method of Iterator instances is similar to Array.prototype.forEach(): it executes a provided function once for each element produced by the iterator.
Syntax
forEach(callbackFn)
Parameters
-
callbackFn - A function to execute for each element produced by the iterator. Its return value is discarded. The function is called with the following arguments:
-
element - The current element being processed.
-
index - The index of the current element being processed.
-
Return value
Description
forEach() iterates the iterator and invokes the callbackFn function once for each element. Unlike most other iterator helper methods, it does not work with infinite iterators, because it is not lazy.
Examples
Using forEach()
new Set([1, 2, 3]).values().forEach((v) => console.log(v)); // Logs: // 1 // 2 // 3
This is equivalent to:
for (const v of new Set([1, 2, 3]).values()) {
console.log(v);
}
Specifications
Browser compatibility
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on iOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
forEach |
122
117–119
|
122
117–119
|
131 |
108
103–105
|
18.4 |
122
117–119
|
131 |
81
78–79
|
18.4 |
26.0
24.0–25.0
|
122
117–119
|
18.4 |
1.1.31 |
1.39 |
22.0.0 |
See also
- Polyfill of
Iterator.prototype.forEachincore-js - es-shims polyfill of
Iterator.prototype.forEach IteratorIterator.prototype.find()Iterator.prototype.map()Iterator.prototype.filter()Iterator.prototype.every()Iterator.prototype.some()Array.prototype.forEach()
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/forEach