JS: Iterator.prototype.toArray

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2025)

iterator.toArray(f)

Convert an Iterator Object 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;
}

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

const xx = xgen.toArray();
console.log(xx);
// [ 1, 2, 3, 4 ]