Array

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

JavaScript
array.reduce(callbackFn, initialValue?)

Parameters

ParameterTypeDescription
callbackFn(accumulator, currentValue, index, array) => TFunction to execute on each element
initialValueTInitial value for the accumulator

Return Value

The value that results from running the reducer across all elements

Examples

Basic Usage
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, n) => acc + n, 0);
console.log(sum); // 10
Practical Example
const items = [
  { name: 'Apple', count: 3 },
  { name: 'Banana', count: 5 }
];
const total = items.reduce((sum, item) => sum + item.count, 0);
console.log(total); // 8
Advanced Usage
const 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

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.