Since March 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
The reduce() method of Iterator instances is similar to Array.prototype.reduce: it executes a user-supplied "reducer" callback function on each element produced by the iterator, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements is a single value.
reduce(callbackFn) reduce(callbackFn, initialValue)
callbackFnA function to execute for each element produced by the iterator. Its return value becomes the value of the accumulator parameter on the next invocation of callbackFn. For the last invocation, the return value becomes the return value of reduce(). The function is called with the following arguments:
accumulatorThe value resulting from the previous call to callbackFn. On the first call, its value is initialValue if the latter is specified; otherwise its value is the first element of the iterator.
currentValueThe value of the current element. On the first call, its value is the first element of the iterator if initialValue is specified; otherwise its value is the second element.
currentIndexThe index position of currentValue. On the first call, its value is 0 if initialValue is specified, otherwise 1.
initialValue OptionalA value to which accumulator is initialized the first time the callback is called. If initialValue is specified, callbackFn starts executing with the first element as currentValue. If initialValue is not specified, accumulator is initialized to the first element, and callbackFn starts executing with the second element as currentValue. In this case, if the iterator is empty (so that there's no first value to return as accumulator), an error is thrown.
The value that results from running the "reducer" callback function to completion over the entire iterator.
TypeErrorThrown if the iterator contains no elements and initialValue is not provided.
See Array.prototype.reduce() for details about how reduce() works. Unlike most other iterator helper methods, it does not work well with infinite iterators, because it is not lazy.
The following example creates an iterator that yields terms in the Fibonacci sequence, and then sums the first ten terms:
function* fibonacci() {
let current = 1;
let next = 1;
while (true) {
yield current;
[current, next] = [next, current + next];
}
}
console.log(
fibonacci()
.take(10)
.reduce((a, b) => a + b),
); // 143
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
reduce |
122117–119 | 122117–119 | 131 | 108103–105 | 18.4 | 122117–119 | 131 | 8178–79 | 18.4 | 26.024.0–25.0 | 122117–119 | 18.4 | 1.1.31 | 1.39 | 22.0.0 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/reduce