Lodash _.add() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.add(augend, addend) handles numbers, strings, and undefined via the shared createMathOperation factory.
  • Why _.add is safe inside _.reduce—both-undefined falls back to 0 (additive identity).
  • The string-concatenation surprise: _.add(1, '2') returns '12', not 3.
  • Why _.add does not fix floating-point precision—myths persist online but the source code calls plain +.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Awareness of the native + operator’s string-vs-number duality and IEEE-754 quirks will make every behavior here feel familiar; see the Math hub for category-wide patterns.

  • Native + operator: 1 + '2' is '12'; lodash mirrors that.
  • Reducer pattern: arr.reduce(callback, initialValue) with a 2-arg callback maps directly to arr.reduce(_.add, 0).

Overview

_.add(augend, addend) is a thin wrapper around the native + operator with one quality-of-life change: undefined operands. If both are undefined the default 0 is returned; if only one is undefined, the other is returned unchanged. Otherwise lodash dispatches to string concatenation (when either operand is a string) or numeric addition.

Number-in / number-out

Both numeric → plain +. No precision correction.

String → concat

Either operand a string → both stringified, joined.

Reducer-safe

Both undefined0, so empty/sparse inputs don’t poison the chain.

Syntax

javascript
_.add(augend, addend)
  • augend: the first number (or string-coercible value) in the addition.
  • addend: the second number (or string-coercible value) in the addition.
  • Returns: the total. A number when both operands are numeric, a string when either is a string, 0 when both are undefined.
1

Lodash docs baseline

The single official example: _.add(6, 4). Identical to 6 + 4.

javascript
import add from "lodash/add";

const total = add(6, 4);
console.log(total);
// => 10
Try it Yourself
2

Undefined handling & reducer use

Both undefined falls back to 0; one undefined returns the other untouched. That’s why _.add works as a drop-in reduce callback even with sparse inputs.

javascript
import add from "lodash/add";

console.log(add(undefined, undefined)); // 0   (additive identity)
console.log(add(undefined, 5));         // 5
console.log(add(5, undefined));         // 5

// Drop-in reducer
const total = [1, undefined, 3, 4].reduce(add, 0);
console.log(total);                     // 8
Try it Yourself
3

Strings concatenate — precision does not

Two facts the lodash docs gloss over: any string operand triggers concatenation, and _.add does nothing to fix 0.1 + 0.2. Both behaviors mirror native +.

javascript
import add from "lodash/add";

console.log(add(1, "2"));     // "12"  (string concat, not 3)
console.log(add(10, "abc"));  // "10abc"
console.log(add("foo", 1));   // "foo1"

console.log(add(0.1, 0.2));   // 0.30000000000000004
console.log(0.1 + 0.2);       // 0.30000000000000004  (same)

// For accurate decimal sums, scale to integers
const cents = add(10, 20);            // 30 cents
console.log((cents / 100).toFixed(2)); // "0.30"
Try it Yourself

📋 _.add vs native +

Inputs_.add(a, b)a + b
6, 41010
0.1, 0.20.300000000000000040.30000000000000004
1, '2''12''12'
undefined, undefined0NaN
undefined, 55NaN
NaN, 5NaNNaN
1n, 2nthrows3n

The only places _.add meaningfully differs from + are the undefined rows—and the BigInt one (lodash throws while native succeeds). Everything else is identical.

Pitfalls to avoid

Myth

It does not fix floating-point

Old tutorials (including the one we’re replacing) claim _.add(0.1, 0.2) returns 0.3. It does not—the source is literally function(augend, addend){ return augend + addend; }. For money, work in integer cents.

Strings

Hidden concatenation

_.add(price, '0.99') sneaks a string in and you get '100.99' instead of 100.99. Normalize inputs with Number(x) or _.toNumber(x) first.

Identity

null isn’t undefined

Only undefined triggers the default-value branch. _.add(null, 5) goes through numeric coercion: 0 + 5 = 5. Same answer, different code path—don’t rely on this for non-numeric nullish.

BigInt

BigInt throws

_.add(1n, 2n) throws “Cannot convert a BigInt value to a number.” Use the native + operator for BigInt math.

❓ FAQ

No. _.add(0.1, 0.2) returns 0.30000000000000004, exactly the same as the native + operator. Lodash does no decimal correction. If you need money-level accuracy, scale to integers (multiply by 100 for cents) or use BigInt / a decimal library.
If only one operand is undefined, lodash returns the other operand unchanged—no addition is performed. _.add(undefined, 5) returns 5. If both are undefined, you get the default identity value 0. This makes _.add safe inside _.reduce(arr, _.add).
When either operand is a string, _.add routes both through baseToString and concatenates—mirroring the native + operator's string-coercion behavior. To force numeric addition, pre-convert with Number(x) or _.toNumber(x).
No. _.add(1n, 2n) throws 'Cannot convert a BigInt value to a number' because lodash routes numeric operands through baseToNumber. Use the native + operator for BigInt arithmetic.
Use import add from "lodash/add"; for ESM or const add = require('lodash/add') in CommonJS. The per-method package keeps your bundle minimal.

Summary

  • Purpose: a thin, safer wrapper around +—the only behavioral difference is undefined handling.
  • Remember: both-undefined0; string operand → concatenation; floating-point precision is unchanged.
  • Next: Lodash _.ceil() covers rounding with positive and negative precision, or jump to the official Lodash docs for _.add.
Did you know?

_.add shares its implementation with _.subtract, _.multiply, and _.divide through a single factory: createMathOperation(op, defaultValue). Only the default changes—0 for add/subtract, 1 for multiply/divide—giving each helper a different additive- or multiplicative-identity behavior when both operands are undefined.

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.

6 people found this page helpful