JS: Iterator.prototype.toArray

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2025)

iterator.toArray(f)

Convert an Iterator to array.

note, Array.from turns a Iterable Object to array.

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