JavaScript Iterator reduce() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Instance method

Definition

iterator.reduce(callback, initialValue) walks each value and updates a running total (the accumulator). When every value has been used, the final accumulator is returned — a number, a string, an object, or whatever you built.

If you pass an initial value, that is the starting accumulator. If you omit it, the first yielded value becomes the start, and reducing begins with the second value. The method consumes the whole iterator.

Remember
iterator.reduce(callback)
iterator.reduce(callback, initialValue)

with initial     → start from that value
without initial  → first value is the start
empty + no init  → TypeError

Callback gets (accumulator, value, index).
Consumes the whole iterator.

Syntax

Call it on an iterator with a reducer function, and usually with an initial value.

JavaScript
const nums = [1, 2, 3, 4, 5].values();

const sum = nums.reduce((acc, n) => acc + n, 0);
console.log(sum);   // 15
CallWhat it does
iterator.reduce(callback, initial)Fold all values into one result, starting from initial.
iterator.reduce(callback)Use the first value as the starting accumulator.
[1, 2, 3].values().reduce((a, b) => a + b, 0)Sum numbers from an array iterator.

A standard JavaScript method (Iterator helpers, Baseline 2025) on every iterator, from Iterator.prototype. No import required. Siblings: Iterator.concat(), Iterator.from(), Iterator.zip(), Iterator.zipKeyed(), chunks(), drop(), every(), filter(), find(), flatMap(), forEach(), includes(), join(), map(), some(), take(), toArray(), windows(), [Symbol.dispose](), [Symbol.iterator]().

Library declarations
JavaScript
// Iterator helpers (Baseline 2025)
Iterator.prototype.reduce(callbackFn)
Iterator.prototype.reduce(callbackFn, initialValue)

// callbackFn    : (accumulator, value, index) => nextAccumulator
// initialValue  : optional starting accumulator
// returns       : the final accumulated value

Parameters

ParameterDescription
callbackFnRequired. Called for each value (after the start is chosen). Return the next accumulator. It receives accumulator, value, and index.
initialValueOptional. The starting accumulator. If omitted, the first yielded value is used instead, and reducing starts at the second value.

Return Value

Return ValueDescription
accumulated valueWhatever your callback builds: often a number or string, but it can be any type. If the iterator is empty and you did not pass initialValue, JavaScript throws a TypeError.

JavaScript Examples

Example 1 — Sum Every Value With an Initial 0

Add 1 through 5 using reduce() with a starting total of 0.

JavaScript
const nums = [1, 2, 3, 4, 5].values();
const sum = nums.reduce((acc, n) => acc + n, 0);

console.log("Sum: " + sum);
Try It Yourself

The Try It page first checks that iterator.reduce() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. Start at 0. The initial accumulator is 0.
  2. Add each value. The callback returns acc + n for 1, 2, 3, 4, and 5.
  3. Final total. 0 + 1 + 2 + 3 + 4 + 5 equals 15.

Example 2 — Reduce Without an Initial Value

Let the first number be the starting accumulator, then multiply the rest.

JavaScript
const nums = [2, 3, 4].values();
const product = nums.reduce((acc, n) => acc * n);

console.log("Product: " + product);
Try It Yourself

The Try It page first checks that iterator.reduce() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. First value starts. With no initial argument, 2 becomes the first accumulator.
  2. Then multiply. Next comes 3 → 2 * 3 = 6, then 4 → 6 * 4 = 24.
  3. Final result. The product of 2, 3, and 4 is 24.

Example 3 — Build a String From Values

Use reduce() to glue numbers into one sentence-like string.

JavaScript
const nums = [1, 2, 3].values();
const text = nums.reduce((acc, n, i) => {
  if (i === 0) return String(n);
  return acc + " then " + n;
}, "");

console.log(text);
Try It Yourself

The Try It page first checks that iterator.reduce() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. Start with "". The initial accumulator is an empty string.
  2. First value. At index 0 the callback returns "1".
  3. Append the rest. Later steps add " then 2" and " then 3".

Browser & Runtime Support

Iterator.prototype.reduce() is an Iterator helper (Baseline Newly available since March 2025). It works in current browsers and in Node.js 22 or newer.

✓ Baseline 2025 · Newly available

iterator.reduce()

Supported in Chrome 122+, Edge 122+, Firefox 131+, Safari 18.4+, Opera 108+ and Node.js 22+. Internet Explorer never got it.

2025 Standard since
Google Chrome 122+ · Desktop & Mobile
Full support
Microsoft Edge 122+ · Chromium based
Full support
Mozilla Firefox 131+ · Desktop & Mobile
Full support
Apple Safari 18.4+ · macOS & iOS
Full support
Opera 108+ · Chromium based
Full support
Samsung Internet 26+ · Android
Supported
Android WebView 122+ · Chromium based
Supported
Node.js 22+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
iterator.reduce() Current browsers

Bottom line: Check support with a typeof test before you use it in older browsers. Prefer chaining helpers and calling toArray() last.

Frequently Asked Questions

It folds every value from an iterator into one accumulated result using your callback. The final accumulator is returned, and the iterator is consumed.
It is strongly recommended. With an initial value, empty iterators are safe and the starting type is clear. Without one, the first value is the start, and an empty iterator throws TypeError.
Three: the current accumulator, the current value, and the index. Return the next accumulator from the callback.
No. It reads the whole iterator before returning. For infinite sources it never finishes. Use take() first if you only want a limited number of values.
The idea matches Array.prototype.reduce(), but it runs on an iterator helper. Feature-detect support in older browsers before you rely on it.

Next: some()

Learn how to test whether at least one value from an iterator passes a check.

some() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful