# .toArray()

> The `.toArray()` JS method of the `Iterator` object collects all values of an iterator into a new array.

## How do I convert an iterator to an array in JavaScript?

The method that consumes the iterator and returns a new `Array` containing all yielded values. Usually called at the end of a chain of lazy iterator helpers.

- Topic: Iterator
- Type: method
- Browser support: Chrome 122+, Chrome Android 122+, Edge 122+, Firefox 131+, Firefox Android 131+, Safari 18.4+, Safari iOS 18.4+

## Example

Wrong:

```js
const names = [...users.values()]
  .filter((u) => u.active)
  .map((u) => u.name);
```

Right:

```js
const names = users.values()
  .filter((u) => u.active)
  .map((u) => u.name)
  .toArray();
```

Calling `toArray()` last keeps `filter()` and `map()` lazy, so one array is created instead of three.

## Common mistakes

- Calling `toArray()` too early and losing the benefit of lazy processing.

## Key points

- `toArray()` is called only on finite iterators or after `take()`.
- It comes at the end of a chain, after `filter()` and `map()` on the iterator.

## Interview questions

### Is `toArray()` the same as `Array.from(iterator)`?

For a finite iterator the result is the same array. `toArray()` reads better at the end of a helper chain.

### Does `toArray()` work with async iterators?

No. Iterator helpers are synchronous; use `Array.fromAsync()` to collect an async iterator.

## Related cards

- [Array.from()](https://wonderium.dev/js/array-from.md)
- [Spread Syntax](https://wonderium.dev/js/spread-syntax.md)
- [.take()](https://wonderium.dev/js/iterator-take.md)
- [Iterator](https://wonderium.dev/js/iterator.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/iterator-to-array
- Last updated: 2026-11-25
