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):

  1. Primitive Conversion (ToPrimitive): Both operands are converted to primitives. Objects, arrays, and dates call their valueOf() or toString() methods.
  2. String Concatenation Priority: If either operand becomes a string, both operands are converted to strings via ToString(), and string concatenation is performed.
  3. 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:

Booleans and null

When paired with numbers, booleans and null undergo numeric coercion:

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:

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.