JavaScript Iterator toArray() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Instance method

Definition

iterator.toArray() reads the iterator until it is done and builds a new Array with those values in order. It is the handy finishing step after a chain of helpers.

Prefer calling it last. Helpers such as map() and filter() stay lazy; collecting too early builds temporary arrays you do not need. It is similar to Array.from(iterator) or [...iterator], but easier to chain.

Remember
iterator.toArray()

pulls every remaining value
returns a new Array

use it LAST after map / filter / take

similar to Array.from(it) or [...it]
empty iterator → []

Syntax

Call toArray() on an iterator when you want a normal array of the remaining values.

JavaScript
const it = [1, 2, 3].values();

const list = it.map((n) => n * 2).toArray();

console.log(list);   // [2, 4, 6]
CallWhat it does
iterator.toArray()Collect every remaining value into a new array.
iterator.map(fn).toArray()Transform lazily, then collect once at the end.
iterator.filter(fn).take(3).toArray()Filter and limit first, then build the array.
Array.from(iterator)Same idea with a different spelling.

An Iterator helper (Baseline Newly available) on Iterator.prototype. No import required. Feature-detect before use in older engines. Siblings: Iterator.concat(), Iterator.from(), Iterator.zip(), Iterator.zipKeyed(), chunks(), drop(), every(), filter(), find(), flatMap(), forEach(), includes(), join(), map(), reduce(), some(), take(), windows(), [Symbol.dispose](), [Symbol.iterator]().

Library declarations
JavaScript
// Iterator helpers (Baseline Newly available)
Iterator.prototype.toArray()

// (no parameters)
// returns: a new Array of the remaining values

Parameters

ParameterDescription
(none)This method takes no parameters. Call it as iterator.toArray().

Return Value

Return ValueDescription
ArrayA new array with every value still left in the iterator, in the order they were produced. An already-finished iterator returns [].

JavaScript Examples

Example 1 — Turn an Iterator into an Array

Start from an array iterator and collect every value with toArray().

JavaScript
const it = ["red", "green", "blue"].values();
const list = it.toArray();

console.log(list.join(", "));
console.log("Is array: " + Array.isArray(list));
Try It Yourself

The Try It page first checks that iterator.toArray() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. Get an iterator. .values() yields the three colour names.
  2. Collect them. toArray() pulls every value into one new array.
  3. Check the type. Array.isArray(list) is true, so normal array methods work.

Example 2 — Finish a Helper Chain

Map and filter first (still lazy), then call toArray() once at the end.

JavaScript
const result = [1, 2, 3, 4, 5].values()
  .map((n) => n * 10)
  .filter((n) => n >= 30)
  .toArray();

console.log(result.join(", "));
Try It Yourself

The Try It page first checks that iterator.toArray() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. Map to tens. Values become 10, 20, 30, 40, 50.
  2. Keep the large ones. filter drops anything below 30.
  3. Collect once. toArray() builds [30, 40, 50] only after the lazy work.

Example 3 — Limit Values Then Collect

Use take() so infinite or long sources stay safe, then finish with toArray().

JavaScript
function* count() {
  let n = 1;
  while (true) {
    yield n;
    n += 1;
  }
}

const first = count().map((n) => n * n).take(4).toArray();

console.log(first.join(", "));
Try It Yourself

The Try It page first checks that iterator.toArray() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. Infinite source. The generator can yield forever, so you must not call bare toArray() on it.
  2. Map and limit. Squares are mapped lazily, then take(4) stops after four values.
  3. Safe collect. toArray() builds [1, 4, 9, 16] and the loop ends.

Browser & Runtime Support

Iterator.prototype.toArray() is an Iterator helper (Baseline Newly available since March 2025). It works in current browsers and in Node.js 22 or newer.

✓ Baseline 2025 · Newly available

iterator.toArray()

Supported in Chrome 122+, Edge 122+, Firefox 131+, Safari 18.4+, Opera 108+ and Node.js 22+. Internet Explorer never got it.

2025 Standard since
Google Chrome 122+ · Desktop & Mobile
Full support
Microsoft Edge 122+ · Chromium based
Full support
Mozilla Firefox 131+ · Desktop & Mobile
Full support
Apple Safari 18.4+ · macOS & iOS
Full support
Opera 108+ · Chromium based
Full support
Samsung Internet 26+ · Android
Supported
Android WebView 122+ · Chromium based
Supported
Node.js 22+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
iterator.toArray() Current browsers

Bottom line: Check support with a typeof test before you use it in older browsers. Prefer chaining helpers and calling toArray() last.

Frequently Asked Questions

It reads every remaining value from the iterator and returns them in a new Array, in order. An empty or finished iterator returns [].
Call it last, after map, filter, take, drop, and similar helpers. That keeps the chain lazy and avoids building temporary arrays too early.
For a single iterator, yes — you get a new array of its values. toArray() is nicer to chain at the end of Iterator helpers.
Only if you limit it first with take() or stop another way. Otherwise it never finishes pulling values.
It is exhausted. Calling next() again usually returns { done: true }. Start a fresh iterator if you need the values again.

Next: windows()

Learn how to walk an iterator with overlapping sliding windows of length n.

windows() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful