New to Rust? Grab our free Rust for Beginners eBook Get it free →
JS Reduce Method: Syntax and Examples

Turning an array of numbers into a total starts with choosing how the running result changes for each number. JavaScript reduce() calls a callback for each assigned element and uses each return value as the next accumulator.
I’ll show how the accumulator changes on each pass and what an initial value does.
TL;DR
JavaScript Array.reduce() calls a reducer for each assigned element and passes each return value into the next call as the accumulator.
- Pass a second argument as the initial accumulator when the result needs a predictable starting value.
- Return the updated accumulator from every callback call.
- Seed reductions that may receive an empty array, or handle that case before calling reduce().
What Is the JavaScript reduce() Method?
I used addition first because its accumulator is a number that can be checked after each callback.
The second argument to reduce() is the seed, not another callback parameter. When omitted, the first assigned element seeds the accumulator and callback calls start at the next assigned element. The callback’s third and fourth parameters are the current index and source array.
A named function works as the callback too. It receives the same arguments and must return the next accumulator.
| Callback value | Meaning on each call |
|---|---|
| accumulator | Initial seed or the previous callback result |
| currentValue | Element at the current assigned index |
| currentIndex | Position of currentValue in the source array |
| array | The array reduce() was called on |
| initialValue | Optional second method argument used to start accumulation |
How to Use reduce() Step by Step
I kept the sum input to three values so the accumulator changes are visible.
Step 1: Sum numbers with an initial value
For addition, zero is a suitable seed because adding zero leaves a number unchanged. I ran a sum, an empty seeded sum and a small frequency count with Node.js v26.7.0 using the source file reduce_demo.js. The output from the command is exactly:
const scores = [4, 7, 9];
const total = scores.reduce((accumulator, score) => accumulator + score, 0);
console.log(total);
const emptyTotal = [].reduce((accumulator, value) => accumulator + value, 0);
console.log(emptyTotal);
const grouped = ["tea", "coffee", "tea"].reduce((counts, drink) => {
counts[drink] = (counts[drink] ?? 0) + 1;
return counts;
}, {});
console.log(grouped);
Here is the exact output:
20
0
{ tea: 2, coffee: 1 }

The empty input returned 0 without invoking its callback, and the count result contained tea: 2 and coffee: 1.
Step 2: Read the accumulator on each pass
With seed 0, the callback returns 4 from the first pair 0 and 4, then 11 from 4 and 7, and finally 20 from 11 and 9.
An array’s push() method returns the new length. When building an array accumulator, return the array itself rather than the return value from push().
Step 3: Build a different result shape
In the count example, each drink name is a key in the accumulator object. The nullish coalescing operator distinguishes a missing property from a stored count of zero, so existing counts can be incremented.
For a one-level flatten, an array accumulator can concatenate each nested array into the result. The dedicated flat() method states that task directly, so check it before writing a reducer.
| Task | Method | Result |
|---|---|---|
| Transform every element | map() | Array with transformed values |
| Keep matching elements | filter() | Array of matches |
| Get one matching element | find() | First match or undefined |
| Carry a running result | reduce() | Value returned by the callback |
| Several branching updates | for…of | Explicit loop state |
For records with prices, a numeric seed lets every record contribute its price. Divide that total by the record count for an average, and check for empty input before dividing. Without a seed, the first object becomes the accumulator rather than a record processed by the callback, so a numeric total can accidentally combine an object with a number.
For minimum or maximum, compare each value with the running number using Math.min() or Math.max(). Define the empty-input result.
Handle Empty Arrays and Other Edge Cases
An empty array without an initial value throws TypeError because no element exists to initialize the accumulator.
I ran the no-seed case from the source file reduce_empty_error.js with Node.js. It printed the TypeError shown below.
try {
[].reduce((accumulator, value) => accumulator + value);
} catch (error) {
console.log(`${error.name}: ${error.message}`);
}
TypeError: Reduce of empty array with no initial value

Mutating a local object accumulator in place avoids allocating a new object for every element.
A filter can remove every element before a following reduce() call, even when the original array had values. If that result can be empty, decide whether the operation should return a default or whether to skip the reduction.
A sparse hole has no assigned value, so reduce skips it. An explicitly stored undefined is visited as a value. reduce() continues across assigned entries, while find() or some() can stop once the callback identifies a match.
| Input or callback issue | Behavior | Safer choice |
|---|---|---|
| Empty array, no seed | Throws TypeError | Supply a seed or handle empty input |
| One element, no seed | Returns that element without calling callback | Seed when callback must process every element |
| Missing slot in sparse array | Callback skips that index | Check whether sparse input is intended |
| Explicit undefined value | Callback receives undefined | Validate or normalize values |
| Callback forgets return | Next accumulator becomes undefined | Return the updated state |
The reduce() method does not mutate the source array itself, though its callback can mutate passed objects. Spreading an accumulating object on each pass copies existing properties repeatedly, which can add cost as the collection grows.
For grouping objects by a key, check whether Object.groupBy() fits the runtime and result you need. In one run over 50,000 integers, reduce() and for…of both returned 1250025000.
The measured times were 0.921 ms for reduce() and 7.309 ms for for…of. A single sample cannot establish a general speed claim. Choose the form that makes the calculation clear, then benchmark representative data if speed matters.
Conclusion
MDN also documents sparse and array-like inputs.
For reference, read MDN’s Array.prototype.reduce() documentation. For neighboring collection tasks, see the site’s JavaScript Array find() method and JavaScript Maps guide.
FAQ
These answers cover common questions about the reducer callback and its initial value.
What does the accumulator do in JavaScript reduce()?
The accumulator holds the initial value or the value returned by the previous callback call. The final accumulator is the result of reduce().
Should I always provide an initial value to reduce()?
An initial value is useful when you need a predictable result shape or when the array may be empty. Choose a seed that matches the operation, such as 0 for addition or an empty object for counts.
Why does reduce() fail on an empty array?
Without an initial value, an empty array has no element to use as the accumulator, so reduce() throws a TypeError.




