Lodash _.sum() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Math

Definition

Computes the sum of the values in array. Part of Lodash Math (stable — not deprecated). Empty, null, and undefined arrays return 0 (unlike _.mean([]), which is NaN).

Pass the numbers or collection shown below. Check the return value before you chain another math helper.

Syntax

Call _.sum. Start with this line.

Lodash
_.sum(array)
CallWhat it does
_.sum(array)Primary signature.

Prefer import sum from "lodash/sum" when you want a smaller bundle.

Library declarations
Lodash
_.sum(array)

Parameters

ParameterDescription
arrayRequired. Array to iterate over

Return Value

Return ValueDescription
*The sum (number), or 0 if empty/nullish

Lodash Examples

Example 1 — Official: Sum an Array

Add every number in the list.

Lodash
console.log(_.sum([4, 2, 8, 6])); // 20
Try It Yourself

How It Works

  1. Input. Flat number array [4, 2, 8, 6].
  2. Add. Lodash folds with native +.
  3. Result. 20 — matches the official docs.
  4. Tip. Prefer this over a hand-rolled loop for clarity.

Example 2 — Empty and Nullish → 0

Missing data does not throw — you get a safe zero.

Lodash
console.log(_.sum([]));   // 0
console.log(_.sum(null)); // 0
console.log(_.sum());     // 0
Try It Yourself

How It Works

  1. Empty array. No values to add → 0.
  2. null / missing. Same short-circuit → 0.
  3. Contrast. _.mean([]) is NaN — different guard.
  4. Use. Safe default when totals may be missing.

Example 3 — Negatives and Decimals

Signed numbers work; floats keep native precision.

Lodash
console.log(_.sum([-2, 5, 1])); // 4
console.log(_.sum([1.5, 2.5])); // 4
console.log(_.sum([0.1, 0.2])); // 0.30000000000000004
Try It Yourself

How It Works

  1. Negatives. -2 + 5 + 1 → 4.
  2. Decimals. 1.5 + 2.5 → 4.
  3. Float trap. 0.1 + 0.2 is not exact in IEEE-754.
  4. Money tip. Sum integer cents, then divide.

Example 4 — String Concatenation Trap

One string can turn the whole sum into text.

Lodash
console.log(_.sum([1, "2", 3]));           // "123"
console.log(_.sum([1, "2", 3].map(Number))); // 6
Try It Yourself

How It Works

  1. Seed. Starts at 1.
  2. String hit. 1 + "2" → "12".
  3. Continue. "12" + 3 → "123".
  4. Fix. Map with Number before summing.

Example 5 — _.sum vs _.sumBy

Use sumBy when values live on objects.

Lodash
var nums = [4, 2, 8, 6];
var rows = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }];

console.log(_.sum(nums));        // 20
console.log(_.sumBy(rows, "n")); // 20
Try It Yourself

How It Works

  1. Flat list. _.sum adds numbers directly.
  2. Objects. _.sumBy(rows, "n") pulls each n.
  3. Same total. Both paths yield 20.
  4. Rule. Numbers → sum; properties → sumBy.

Browser & Runtime Support

_.sum is a Lodash Math helper available in 4.x (and earlier builds). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native equivalents use array.reduce((a, b) => a + b, 0); Lodash additionally returns 0 for empty/nullish input.

✓ Stable · Lodash 4.x

_.sum()

Available in [email protected] full builds and the lodash/sum modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.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
_.sum Lodash 4.x

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import sum from "lodash/sum" for smaller builds. Keep the list numeric — strings concatenate.

Frequently Asked Questions

_.sum(array) computes the sum of the values in array. Official docs: _.sum([4, 2, 8, 6]) → 20. Empty, null, and undefined arrays return 0.
_.sum short-circuits empty/nullish input to 0. _.mean divides sum by length, so empty becomes 0 / 0 → NaN.
Lodash uses native +. After the first number, hitting a string switches to string concatenation: 1 + '2' → '12', then '12' + 3 → '123'. Coerce with _.sum(arr.map(Number)) when needed.
No. _.sum([0.1, 0.2]) can still show 0.30000000000000004 — same as native +. For money, work in integer cents or use a decimal library.
Use _.sumBy when items are objects and you need to sum a property or iteratee result — for example _.sumBy(users, 'age'). _.sum expects a flat list of numbers.
Use import sum from "lodash/sum" or require("lodash/sum") so bundlers can tree-shake. Full build: import _ from "lodash" then _.sum.

Next: _.sumBy()

Sum values from objects using a property or iteratee.

_.sumBy() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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