W3cubDocs

/JavaScript

Iterator.prototype.forEach()

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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:

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 well 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

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android Deno Node.js
forEach 117 117 No 103 No 117 No 78 No No 117 No No

See also

© 2005–2023 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