Lodash _.add() method
What you’ll learn
- How
_.add(augend, addend)handles numbers, strings, andundefinedvia the sharedcreateMathOperationfactory. - Why
_.addis safe inside_.reduce—both-undefined falls back to0(additive identity). - The string-concatenation surprise:
_.add(1, '2')returns'12', not3. - Why
_.adddoes 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 toarr.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 undefined → 0, so empty/sparse inputs don’t poison the chain.
Syntax
_.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
numberwhen both operands are numeric, astringwhen either is a string,0when both areundefined.
Lodash docs baseline
The single official example: _.add(6, 4). Identical to 6 + 4.
import add from "lodash/add";
const total = add(6, 4);
console.log(total);
// => 10 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.
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 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 +.
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" 📋 _.add vs native +
| Inputs | _.add(a, b) | a + b |
|---|---|---|
6, 4 | 10 | 10 |
0.1, 0.2 | 0.30000000000000004 | 0.30000000000000004 |
1, '2' | '12' | '12' |
undefined, undefined | 0 | NaN |
undefined, 5 | 5 | NaN |
NaN, 5 | NaN | NaN |
1n, 2n | throws | 3n |
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
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.
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.
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 throws
_.add(1n, 2n) throws “Cannot convert a BigInt value to a number.” Use the native + operator for BigInt math.
❓ FAQ
Summary
- Purpose: a thin, safer wrapper around
+—the only behavioral difference isundefinedhandling. - Remember: both-
undefined→0; 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.
_.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.
6 people found this page helpful
