Syntax
arr.reduce((accumulator, current) => newAccumulator, initialValue)Examples
Summing Values
The classic reduce() example - accumulating a total.
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((total, n) => total + n, 0);
console.log(sum); // 15
// Without an initial value, the first element becomes the starting accumulator
const sumNoInitial = numbers.reduce((total, n) => total + n);
console.log(sumNoInitial); // 15 - same result here, but riskier on empty arraysGrouping Data
Using reduce() to transform an array into a grouped object.
const people = [
{ name: "Alice", dept: "Engineering" },
{ name: "Bob", dept: "Sales" },
{ name: "Charlie", dept: "Engineering" }
];
const byDept = people.reduce((groups, person) => {
const key = person.dept;
if (!groups[key]) groups[key] = [];
groups[key].push(person.name);
return groups;
}, {});
console.log(byDept);
// { Engineering: ["Alice", "Charlie"], Sales: ["Bob"] }Counting Occurrences
Another common reduce() pattern - tallying values.
const votes = ["yes", "no", "yes", "yes", "no"];
const tally = votes.reduce((counts, vote) => {
counts[vote] = (counts[vote] || 0) + 1;
return counts;
}, {});
console.log(tally); // { yes: 3, no: 2 }Best practices
- Always provide an initial value as the second argument - it avoids errors on empty arrays and makes the starting state explicit
- Remember to return the accumulator from the callback every time - forgetting this is the single most common reduce() bug
- Use reduce() for genuinely aggregate operations (sums, grouping, flattening) - for simple transformations, map()/filter() are usually clearer
- Consider breaking a very complex reduce() into a named function passed as the callback, for readability
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Array Basics
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().