JS: Iterator.prototype.reduce

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2025)

iterator.reduce(args)

Similar to Array.prototype.reduce , but works on iterator.

// define a generator function
function* gf() {
 for (let x of [1, 2, 3, 4, 5]) yield x;
}

const ff = (x, y) => `f(${x} ${y})`;

// create a generator. a generator is both iterable and iterator
const xgen = gf();

const xresult = xgen.reduce(ff);

console.assert(
 xresult === "f(f(f(f(1 2) 3) 4) 5)",
);