Iterator.prototype.forEach()
MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونهکد
The **forEach()** method of {{jsxref("Iterator")}} instances is similar to {{jsxref("Array.prototype.forEach()")}}: it executes a provided function once for each element produced by the iterator.
Syntax
js-nolint
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
{{jsxref("undefined")}}.
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
// 3This is equivalent to:
js
for (const v of new Set([1, 2, 3]).values()) {
console.log(v);
}Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- [Polyfill of
Iterator.prototype.forEachincore-js](https://core-js.io/docs/features/ecmascript/iterator) - [es-shims polyfill of
Iterator.prototype.forEach](https://www.npmjs.com/package/es-iterator-helpers) - {{jsxref("Iterator")}}
- {{jsxref("Iterator.prototype.find()")}}
- {{jsxref("Iterator.prototype.map()")}}
- {{jsxref("Iterator.prototype.filter()")}}
- {{jsxref("Iterator.prototype.every()")}}
- {{jsxref("Iterator.prototype.some()")}}
- {{jsxref("Array.prototype.forEach()")}}