Iterator.prototype.forEach()
Baseline 2025Newly available
Since March 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
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
js
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:
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()
js
new Set([1, 2, 3]).values().forEach((v) => console.log(v));
// Logs:
// 1
// 2
// 3
This is equivalent to:
js
for (const v of new Set([1, 2, 3]).values()) {
console.log(v);
}
Specifications
Specification |
---|
Iterator Helpers # sec-iteratorprototype.foreach |