JS: Iterator.prototype.toArray

By Xah Lee. Date: .

(new in JS: ECMAScript 2025)

iterator.toArray(f)

Convert an iterator to array.

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

similar to JS: Array.from

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

// get the first even number
console.log(
  JSON.stringify(
    gf().toArray(),
  ) === `[1,2,3,4]`,
);
// true

JavaScript. iterator helpers