Passing Non-Arrays to Lodash Array Methods
When non-array values are passed to array methods in the Lodash
JavaScript library, the functions fail safely instead of throwing
runtime exceptions. Unlike native JavaScript array methods that throw
errors when operating on incompatible types, Lodash employs defensive
type-checking to return fallback values—typically an empty array,
undefined, or the unmodified input—depending on the
specific method and argument provided.
Safe Fallbacks for
null and undefined
In standard JavaScript, calling an array method on null
or undefined results in a TypeError. Lodash
methods guard against these values internally. When an array method
receives null or undefined, it safely resolves
to a default output:
- Transformation and slicing methods: Functions like
_.chunk,_.compact,_.drop, and_.flattenreturn an empty array ([]). - Accessor methods: Functions designed to return a
single element, such as
_.heador_.last, returnundefined.
// Native JavaScript throws:
// null.slice(1); // TypeError: Cannot read properties of null
// Lodash fails safely:
_.drop(null, 2); // Returns []
_.head(undefined); // Returns undefinedHandling Primitives (Numbers and Booleans)
Primitive types like numbers, booleans, and symbols do not have
array-like structures. When passed into Lodash array methods, Lodash
checks if the value can be converted or indexed. Because these
primitives lack an iterable structure and a valid length
property, Lodash treats them as empty:
_.compact(123)returns[]_.flatten(true)returns[]_.initial(42)returns[]
Handling Array-Like Values and Strings
Lodash distinguishes between purely non-array values and "array-like"
values (objects containing a numeric length property, such
as strings, the arguments object, or custom DOM
collections).
For methods specifically categorized under Lodash’s
Array module:
- Strings: While strings possess a
lengthproperty and indexed elements, most strict Lodash array methods treat them as non-arrays and return an empty array (e.g.,_.drop('hello')returns[]). However, collection-level methods like_.mapor_.filterwill iterate over each character. - Plain Objects: Plain objects without a valid
lengthproperty return empty arrays when passed to array methods. If an object mimics an array structure{ 0: 'a', 1: 'b', length: 2 }, it may still be treated as an empty array by strict array functions, as Lodash specifically checks for native arrays or casts them depending on internal method requirements.
Benefits and Trade-Offs
The primary benefit of Lodash's handling of non-array inputs is application resilience. UI components and data-processing pipelines do not crash unexpectedly when processing uninitialized or malformed API responses.
The trade-off is the potential masking of logical bugs. Because
Lodash handles invalid types silently, passing an incorrect data
structure may not generate an immediate error, causing failures further
down the execution pipeline when an empty array or
undefined is not explicitly anticipated.