Array.prototype.reduce
Executes a user-supplied reducer callback function on each element of the array, passing in the return value from the calculation on the preceding element
Syntax
array.reduce(callbackFn, initialValue?)Parameters
| Parameter | Type | Description |
|---|---|---|
| callbackFn | (accumulator, currentValue, index, array) => T | Function to execute on each element |
| initialValue | T | Initial value for the accumulator |
Return Value
The value that results from running the reducer across all elements
Examples
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, n) => acc + n, 0);
console.log(sum); // 10const items = [
{ name: 'Apple', count: 3 },
{ name: 'Banana', count: 5 }
];
const total = items.reduce((sum, item) => sum + item.count, 0);
console.log(total); // 8const words = ['Hello', ' ', 'World'];
const sentence = words.reduce((acc, w) => acc + w, '');
console.log(sentence); // 'Hello World'Understanding Array.prototype.reduce
The Array.prototype.reduce method in JavaScript executes a user-supplied reducer callback function on each element of the array, passing in the return value from the calculation on the preceding element. It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.
The method signature is array.reduce(callbackFn, initialValue?). It accepts 2 parameters: callbackFn, initialValue. When called, it returns the value that results from running the reducer across all elements. Understanding when and how to use reduce() helps you write more expressive, readable code.
Common use cases for Array.prototype.reduce include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-reduceright, array-map, array-filter, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Array.prototype.reduce is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Array.prototype.reduceRightApplies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value
Array.prototype.mapCreates a new array populated with the results of calling a provided function on every element in the calling array
Array.prototype.filterCreates a shallow copy of a portion of a given array, filtered down to just the elements that pass the test implemented by the provided function
Array.prototype.forEachExecutes a provided function once for each array element
More Array Methods
Other methods in the Array object
Related Tools
More Array Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.