Underscore.js _.reduce() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Also known as inject and foldl, reduce boils a list of values down to a single value. Memo is the initial state; each step returns the next memo (official Underscore docs). Stable Collection helper — not deprecated.

Pass the required arguments shown below. Underscore collection helpers usually return a new value — check whether the source is mutated.

Syntax

Call _.reduce. Start with this line.

Underscore
_.reduce(list, iteratee, [memo])
CallWhat it does
_.reduce(list, iteratee, [memo])Primary signature.
_.reduce(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.reduce(list, iteratee, [memo])

Parameters

ParameterDescription
listRequired. Collection to fold
iterateeRequired. (memo, value, index|key, list) → nextMemo
memoOptional. Starting accumulator (*recommended)
contextOptional. this for the iteratee

Return Value

Return ValueDescription
*Final accumulated value

Underscore.js Examples

Example 1 — Sum with Memo

Fold numbers into a total — the official docs pattern.

Underscore
var sum = _.reduce([1, 2, 3], function (memo, num) {
  return memo + num;
}, 0);
// → 6
Try It Yourself

How It Works

  1. Start at 0. Memo seeds the sum.
  2. Add each number. Iteratee returns memo + num.
  3. Carry forward. Each return becomes the next memo.
  4. Final value. After 1, 2, and 3 you get 6.

Example 2 — No Initial Memo

Omit memo and the first element seeds the accumulator.

Underscore
var sum = _.reduce([1, 2, 3], function (memo, num) {
  return memo + num;
});
// → 6  (starts with memo=1, then +2, +3)
Try It Yourself

How It Works

  1. Seed from list. First value 1 becomes memo.
  2. Skip first call. Iteratee starts on the second element.
  3. Same sum here. 1+2+3 still yields 6.
  4. Caution. Empty lists need an explicit memo or reduce fails.

Example 3 — Tally Counts

Accumulate into an object — memo is not always a number.

Underscore
var fruits = ["apple", "pear", "apple", "fig", "pear", "apple"];

var counts = _.reduce(fruits, function (memo, fruit) {
  memo[fruit] = (memo[fruit] || 0) + 1;
  return memo;
}, {});
// → { apple: 3, pear: 2, fig: 1 }
Try It Yourself

How It Works

  1. Start with {}. Object memo holds counts.
  2. Bump each key. Increment or start at 1.
  3. Return the object. Same memo reference carries forward.
  4. One pass. Frequency map without a second loop.

Example 4 — Flatten with Concat

Reduce can build a new array by concatenating each chunk.

Underscore
var nested = [[0, 1], [2, 3], [4, 5]];

var flat = _.reduce(nested, function (memo, pair) {
  return memo.concat(pair);
}, []);
// → [0, 1, 2, 3, 4, 5]
Try It Yourself

How It Works

  1. Memo is []. Empty array seeds the result.
  2. Concat each pair. Append [0,1], then [2,3], then [4,5].
  3. New arrays. concat returns a fresh array each step.
  4. Left order. For right-to-left flatten, see _.reduceRight.

Browser & Runtime Support

_.reduce ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.reduce()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded. Aliases inject / foldl included.

100% With Underscore 1.x
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
_.reduce Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]). Native alternative: Array.prototype.reduce on real arrays.

Frequently Asked Questions

_.reduce(list, iteratee, [memo], [context]) boils a list down to a single value. Memo is the running accumulator; each step returns the next memo. Also known as inject and foldl.
No. inject and foldl are aliases of reduce — same function, different names.
Four: memo, value, index (or key), and the full list — (memo, value, index, list) for arrays.
The first element becomes the starting memo, and the iteratee begins on the second element.
_.map returns one result per item (a new array). _.reduce combines everything into one accumulated value.
When you need to fold from last to first. For left-to-right accumulation, stick with _.reduce.

Next: _.reduceRight()

Fold a collection from the last element to the first.

_.reduceRight() →

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.

10 people found this page helpful