Array.reduce()
Reduces an array to a single value using an accumulator function.
array.reduce(reducerFn, initialValue?)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Reduces an array to a single value using an accumulator function.
The reduce() method executes a user-supplied callbackFn on each element of the array in sequence, passing in the return value from the calculation on the preceding element. The final result is a single value representing the accumulation of all elements. In terms of performance, reduce() has a time complexity of O(n) as it visits every element exactly once. A critical edge case exists when calling reduce() on an empty array: if an initialValue is not provided, a TypeError will be thrown. Conversely, if the array has only one element (with no initialValue) or if the array is empty but an initialValue is provided, that single value will be returned without calling the callbackFn. When an initialValue is supplied, the accumulator starts as that value and the current value starts at index 0. Without an initialValue, the accumulator defaults to the first element and processing starts at index 1.
Quick reference
Syntax
array.reduce(reducerFn, initialValue?)
Inputs
Parameters
See it in practice
Examples
Summing an Array of Numbers
const prices = [29.99, 5.00, 10.50];
const total = prices.reduce(function(acc, curr) {
return acc + curr;
}, 0);45.49
A fundamental use case where numbers are added to an initial accumulator of 0.
Grouping Objects by Property
const inventory = [
{ name: 'apples', type: 'fruit' },
{ name: 'bananas', type: 'fruit' },
{ name: 'carrots', type: 'vegetable' }
];
const grouped = inventory.reduce(function(acc, item) {
const key = item.type;
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(item);
return acc;
}, {});{"fruit": [{"name": "apples", "type": "fruit"}, {"name": "bananas", "type": "fruit"}], "vegetable": [{"name": "carrots", "type": "vegetable"}]}
Transforms an array into an object where keys are categories and values are arrays of items.
Flattening Nested Arrays
const nested = [[1, 2], [3, 4], [5, 6]];
const flat = nested.reduce(function(acc, curr) {
return acc.concat(curr);
}, []);[1, 2, 3, 4, 5, 6]
Combines multiple sub-arrays into a single flat array by concatenating them into the accumulator.
Debug faster
Common Errors
TypeError
Cause: Attempting to reduce an empty array without providing an initialValue.
Fix: Always provide an initialValue (like 0, [], or {}) if there is any possibility the source array could be empty.
const result = [].reduce((acc, val) => acc + val);LogicError
Cause: Forgetting to return the accumulator value from the callback function.
Fix: Ensure the callbackFn always returns the accumulator so the next iteration receives the updated state.
const total = [1, 2, 3].reduce((acc, val) => { acc + val; }, 0); // Returns undefinedRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Reduces an array to a single value using an accumulator function.
reducer: Computes the next accumulator value. initialValue: Starting accumulator value.
TypeError: Always provide an initialValue (like 0, [], or {}) if there is any possibility the source array could be empty. LogicError: Ensure the callbackFn always returns the accumulator so the next iteration receives the updated state.