JavaScript Array reduce() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

Array.prototype.reduce(callback, initialValue?) folds an array into one value by updating an accumulator left to right. Use it for sums, max, grouping, and one-level flattens.

Remember
array.reduce((acc, value, index, array) => nextAcc, initialValue?)

acc starts as initialValue (recommended)
each step returns the next acc
final return = last acc

empty array + no initialValue → TypeError
read-only · ES5

Syntax

Call array.reduce. Start with this line.

JavaScript
arr.reduce(fn, init?)
CallWhat it does
arr.reduce(fn, init?)Primary signature.
array.reduce(...)Call with the arguments your case needs.

Built into JavaScript. Call reduce(...) on an array instance — no import required.

Library declarations
JavaScript
arr.reduce(fn, init?)

Parameters

ParameterDescription
CallbackRequired. Returns next accumulator
initialValueOptional. Starting acc; avoids empty-array errors

Return Value

Return ValueDescription
*Final accumulator

JavaScript Examples

Example 1 — Sum All Numbers

Start at 0 and add each element to the accumulator.

JavaScript
const numbers = [1, 2, 3, 4, 5];

const sum = numbers.reduce((acc, num) => {
  return acc + num;
}, 0);

console.log(sum);
// 15
Try It Yourself

How It Works

  1. Set initialValue to 0. Sums should not start from the first element alone.
  2. Add each number. acc + num becomes the next accumulator.
  3. Walk left to right. 0→1→3→6→10→15.
  4. Return the total. sum is 15.

Example 2 — Find the Maximum Value

Without initialValue, the first element seeds the accumulator.

JavaScript
const scores = [88, 92, 76, 95, 84];

const max = scores.reduce((acc, score) => {
  return score > acc ? score : acc;
});

console.log(max);
// 95
Try It Yourself

How It Works

  1. No init provided. First score 88 becomes the starting acc.
  2. Compare each score. Keep the larger of acc and score.
  3. Track the winner. 95 beats every other value.
  4. Empty caution. On [] this pattern throws — pass an init or check length.

Example 3 — Flatten a Nested Array (One Level)

Concatenate each inner array into an accumulator starting as [].

JavaScript
const nested = [[1, 2], [3, 4], [5, 6]];

const flat = nested.reduce((acc, chunk) => {
  return acc.concat(chunk);
}, []);

console.log(flat);
// [1, 2, 3, 4, 5, 6]
Try It Yourself

How It Works

  1. Start with []. The accumulator is an empty array.
  2. Concat each chunk. [1,2], then [3,4], then [5,6].
  3. One level only. Deeper nests stay nested — use flat() for more.
  4. Result is a new list. Original nested is unchanged.

Browser & Runtime Support

Array.prototype.reduce() has been available since ES5 (2009). It is widely supported for accumulation and grouping patterns.

✓ Baseline · ES5

Array.prototype.reduce()

Supported in Chrome 3+, Firefox 3+, Safari 4+, Edge (all versions), IE 9+, and all modern Node.js versions.

99% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.reduce() Excellent

Bottom line: Safe for modern projects and IE9+. No polyfill needed for typical targets.

Frequently Asked Questions

reduce() walks the array left to right, running a callback on each element. The callback returns an accumulator that carries forward; the final accumulator is the return value.
No. reduce() only reads the array. Your callback can still mutate objects you put in the accumulator if you choose to.
initialValue is the starting accumulator for the first callback call. Without it on an empty array, reduce throws TypeError. Prefer providing one for clear, safe code.
callback(accumulator, currentValue, currentIndex, array). Return the next accumulator from each step.
forEach() returns undefined and is for side effects. reduce() returns one combined result built from all elements.
reduce() is ES5 (2009). It works in all modern browsers and Internet Explorer 9+.

Next: reduceRight()

Learn how to reduce an array from right to left.

reduceRight() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful