How Lodash _.sum Handles Mixed Type Coercion
The Lodash _.sum function calculates the total of an
array's elements by delegating the reduction logic to an internal helper
called baseSum. Rather than explicitly converting every
element to a numeric primitive via functions like Number(),
Lodash relies directly on JavaScript’s native binary addition operator
(+). As a result, evaluating dynamically typed mixed arrays
in _.sum triggers the standard ECMAScript addition coercion
rules, leading to either arithmetic addition, numeric promotion, or
string concatenation depending on element order and type.
The Internal Mechanics of
_.sum
Under the hood, Lodash implements _.sum using a
straightforward loop inside baseSum. Its core accumulation
step behaves equivalent to the following logic:
result = result === undefined ? current : (result + current);Before combining values, Lodash explicitly checks whether each
element is undefined. If an element is
undefined, it is ignored and does not alter the
accumulator.
For all other data types, Lodash does not perform type checking or
sanitize inputs into numbers. The accumulated result and
the current element are directly combined using the native
+ operator.
ECMAScript Coercion Rules in Action
Because _.sum uses the native + operator,
mixed-type evaluation follows the ECMAScript specification for addition
(ApplyStringOrNumericBinaryOperator):
- Primitive Conversion (
ToPrimitive): Both operands are converted to primitives. Objects, arrays, and dates call theirvalueOf()ortoString()methods. - String Concatenation Priority: If either operand
becomes a string, both operands are converted to strings via
ToString(), and string concatenation is performed. - Numeric Evaluation: If neither operand is a string,
both are converted to numeric primitives via
ToNumeric(). Arithmetic addition is then applied.
How Different Types Behave in Mixed Arrays
Because the accumulator carries over its intermediate type from left to right, the ordering of types in a mixed array directly dictates the final outcome.
Strings and Concatenation
If a string is encountered, the accumulator turns into a string for all subsequent operations:
_.sum([10, 20, '5']): The first two numbers yield30. Adding'5'coerces30to a string, returning'305'._.sum(['5', 10, 20]): The initial value is'5'. Adding10produces'510', and adding20produces'51020'.
Booleans and null
When paired with numbers, booleans and null undergo
numeric coercion:
truecoerces to1.falsecoerces to0.nullcoerces to0.
For example, _.sum([5, true, null, 4]) evaluates to
10 because 5 + 1 + 0 + 4 = 10. However, if a
string appears earlier in the array, true and
null are converted to "true" and
"null" instead.
NaN Propagation
If an element cannot be coerced into a meaningful number (such as an
unparseable object or literal NaN) during numeric addition,
it evaluates to NaN. Once the accumulator becomes
NaN, subsequent numeric additions remain NaN,
though encountering a subsequent string will concatenate
"NaN" with that string.
Objects and Arrays
Plain objects coerce to "[object Object]", triggering
string concatenation. Arrays are coerced via their
join(',') representation:
_.sum([1, [2, 3]])coerces[2, 3]to"2,3", resulting in the string"12,3"._.sum([1, {}])results in"1[object Object]".
Summary
Lodash's _.sum does not implement custom mathematical
coercion algorithms. It inherits JavaScript’s implicit type coercion via
the + operator, making it sensitive to element types and
array ordering whenever non-numeric values are present.