# Iterator.from()

> The `Iterator.from()` static JS method creates an `Iterator` object from an iterable or iterator-like object.

## How do I use Iterator.from() in JavaScript?

The static method that converts an iterable or an object with a `next()` method into a proper `Iterator`. Allows using iterator helper methods on any iterable source.

- 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 counter = {
  i: 0,
  next() {
    return { done: this.i > 2, value: this.i++ };
  },
};
counter.map((x) => x * 2); // TypeError
```

Right:

```js
const counter = {
  i: 0,
  next() {
    return { done: this.i > 2, value: this.i++ };
  },
};
Iterator.from(counter).map((x) => x * 2).toArray(); // [0, 2, 4]
```

An object with only `next()` has no helper methods; `Iterator.from()` wraps it so `map()` and `toArray()` work.

## Common mistakes

- Expecting `Iterator.from()` to copy values, while it wraps the same source.

## Key points

- `Iterator.from()` wraps objects that only have a `next()` method, so they get helper methods.
- Built-in iterators like `map.values()` or generators are not wrapped, they already have helpers.

## Interview questions

### What is the difference between `Iterator.from()` and `Array.from()`?

`Array.from()` reads every value into a new array right away. `Iterator.from()` returns a lazy iterator over the same source, without copying.

### Can I pass a string to `Iterator.from()`?

Yes. `Iterator.from('abc')` yields the string one code point at a time.

## Related cards

- [Iterator](https://wonderium.dev/js/iterator.md)
- [Array.from()](https://wonderium.dev/js/array-from.md)
- [Generator](https://wonderium.dev/js/generator.md)

---

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