JS: Iterator.prototype.forEach

By Xah Lee. Date: . Last updated: .

(new in JS: ECMAScript 2025)

iterator.forEach(f)

Normally you can use this method on any Iterable, because all standard iterable objects are also Iterator.

  • f is given args:
  • currentElement
  • currentIndex

similar to JS: Array.prototype.forEach

// define a generator function
function* gf() {
  for (let x of Array(5).fill(0)) yield x;
}

// create a generator (is an iterable)
const gg = gf();

// use method forEach
const hh = gg.forEach((x, i) => console.log(x, i));
/*
0 0
0 1
0 2
0 3
0 4
*/

console.log(hh === undefined);
// true

JavaScript. iterator helpers