# Iterator.map()

> The `.map()` JS method of the `Iterator` object returns a new iterator that yields transformed values.

## How do I map over an iterator lazily in JavaScript?

The method that returns a new iterator where each value is the result of calling a callback on the values of the original iterator. Values are computed lazily, only when requested.

- 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 = new Map([[1, 'Ann'], [2, 'Bob']]);
const upper = Array.from(names.values())
  .map((n) => n.toUpperCase());
```

Right:

```js
const names = new Map([[1, 'Ann'], [2, 'Bob']]);
const upper = names.values()
  .map((n) => n.toUpperCase())
  .toArray();
```

`map()` runs on the iterator directly, so no intermediate array is built before mapping.

## Common mistakes

- Expecting `map()` on an iterator to return an array.

## Key points

- Values from a `Map`, `Set` or generator are mapped on the iterator, without `Array.from()` first.
- The result is consumed with `toArray()`, `for...of` or another helper, otherwise nothing runs.

## Interview questions

### Does iterator `map()` return an array?

No. It returns a new lazy iterator; call `toArray()` or loop with `for...of` to get the values.

### What is the second argument of the iterator `map()` callback?

A counter that starts at 0. Unlike array `map()`, there is no third argument with the source.

## Related cards

- [Iterator.filter()](https://wonderium.dev/js/iterator-filter.md)
- [.toArray()](https://wonderium.dev/js/iterator-to-array.md)
- [Array.map()](https://wonderium.dev/js/array-map.md)
- [Iterator](https://wonderium.dev/js/iterator.md)

---

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