The reduce() method folds an array into a single value by running a callback on each element and carrying an accumulator forward. This tutorial covers syntax, initialValue, five examples (sum, max, flatten, object build, average), and comparisons with forEach and reduceRight.
01
Syntax
reduce(fn, init)
02
Accumulator
Carry state
03
initialValue
Start point
04
One result
Any type
05
Left → right
Order matters
06
ES5
Wide support
Fundamentals
Introduction
Sometimes you do not want a new array—you want one number, one object, or one string built from many pieces. Summing prices, finding the maximum score, or merging nested lists are all reduce() jobs.
Think of the accumulator as a running total or bucket. Each step updates it; the final return is your answer. The source array is not modified.
Concept
Understanding the reduce() Method
array.reduce(callback, initialValue?) calls callback(accumulator, currentValue, index, array) for each element, left to right. Whatever you return becomes the accumulator for the next step.
If initialValue is provided, the first call uses it as the accumulator and starts at index 0. If omitted, the first element becomes the initial accumulator and iteration starts at index 1.
💡
Beginner Tip
Pass an initialValue (like 0 for sums or [] for building arrays) until you are comfortable with the no-initialValue rules—especially on empty arrays, which throw without one.
Sum is 100; divided by 4 elements gives 25. Guard against empty arrays before dividing.
Applications
🚀 Common Use Cases
Totals and stats — sum, product, min, max, average.
Grouping — build objects or Maps from arrays.
Flattening — merge nested arrays one level.
Pipeline composition — fold functions or values.
Single-pass transforms — filter + map in one loop (advanced).
Cart totals — sum price * qty across items.
🧠 How reduce() Runs
1
Set accumulator
Use initialValue or first element.
Start
2
Call callback
Pass acc, current value, index, array.
Step
3
Update accumulator
Return value becomes next acc.
Fold
4
🎯
Return final value
Last accumulator after all elements.
Important
📝 Notes
Does not mutate the source array.
Always return the accumulator from your callback.
Empty array without initialValue throws TypeError.
Result type can be anything: number, string, object, array.
Order matters—use reduceRight for right-to-left.
Prefer readable loops when reduce hurts clarity.
ES5—excellent support including IE9+.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.reduce()Excellent
Bottom line: Safe to use everywhere except very old IE8 environments. No polyfill needed for modern projects.
Wrap Up
Conclusion
reduce() folds a list into one value by updating an accumulator at each step. Master sum and max first, then explore objects and flattening. Provide initialValue when it makes intent clear.
Next, learn reduceRight() to fold from the end toward the start.
Pass initialValue for sums (0) and builds ([], {})
Always return the accumulator from the callback
Use descriptive names: acc, total, counts
Split complex logic: sum first, divide for average
Prefer flat() when flattening is the only goal
❌ Don’t
Forget to return inside the callback
Call reduce on empty arrays without initialValue
Force reduce when a simple loop reads better
Mutate the source array inside reduce unnecessarily
Assume reduce always returns a number
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Array.reduce()
Fold many elements into one accumulated result.
5
Core concepts
📝01
Syntax
reduce(fn, init)
API
📈02
Accumulator
Running state.
Core
003
initialValue
Safe start.
Param
🎯04
One result
Any type.
Output
→05
Direction
Left to right.
Order
❓ 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 method's return value.
No. reduce() only reads the array. However, your callback can mutate objects you store in the accumulator if you choose to.
initialValue is the starting accumulator passed to the first callback call. Without it on an empty array, reduce throws TypeError. With mixed types, always provide one for predictable results.
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 has been supported in Internet Explorer 9+.
Did you know?
reduce() is so flexible that map and filter can be implemented with it—but in real code, use the dedicated methods unless you truly need a single custom pass.