# Iterator.reduce()

> The `.reduce()` JS method of the `Iterator` object reduces the values of an iterator into a single value.

## How do I reduce an iterator to a single value in JavaScript?

The method that consumes the iterator, applying a callback to an accumulator and each value to produce a single result.

- 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 total = cart.values()
  .map((item) => item.price)
  .reduce((sum, price) => sum + price);
```

Right:

```js
const total = cart.values()
  .map((item) => item.price)
  .reduce((sum, price) => sum + price, 0);
```

Without an initial value, `reduce()` throws a `TypeError` on an empty cart; with `0` it returns `0`.

## Common mistakes

- Calling `reduce()` without an initial value on an empty iterator, which throws a `TypeError`.

## Key points

- An initial value is passed when the iterator can be empty.
- `reduce()` is called on the iterator instead of converting it to an array first.

## Interview questions

### What arguments does the iterator `reduce()` callback get?

The accumulator, the current value and a counter starting at 0. Unlike array `reduce()`, there is no argument with the source.

### Can I call `reduce()` on an infinite iterator?

Only after limiting it with `take()`, because `reduce()` reads values until the iterator is done.

## Related cards

- [Array.reduce()](https://wonderium.dev/js/array-reduce.md)
- [Iterator.map()](https://wonderium.dev/js/iterator-map.md)
- [Iterator](https://wonderium.dev/js/iterator.md)
- [Math.sumPrecise()](https://wonderium.dev/js/math-sum-precise.md)

---

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