Lodash isArrayLike vs isArrayLikeObject
In the Lodash utility library, both _.isArrayLike and
_.isArrayLikeObject are used to determine whether a given
value mimics the structure of an array, but they differ fundamentally in
how they treat primitive types. While _.isArrayLike returns
true for any value with a valid length
property (including primitive strings), _.isArrayLikeObject
adds a strict secondary check requiring the value to also be a non-null
JavaScript object. This article clarifies the mechanics behind both
methods, highlights their core behavioral differences, and provides
practical guidance on when to use each.
Defining "Array-Like" in Lodash
Before differentiating the two methods, it is essential to understand how Lodash defines an "array-like" value. In JavaScript, an entity is considered array-like if it meets two criteria:
- It is not a function.
- It has a
lengthproperty that is a non-negative integer less than or equal toNumber.MAX_SAFE_INTEGER(9007199254740991).
Values meeting these criteria include standard arrays,
arguments objects, DOM collections like
NodeList or HTMLCollection, and primitive
strings.
How _.isArrayLike
Works
The _.isArrayLike method checks only for the array-like
structural contract. It verifies that the passed target is not a
function and that its length property is a valid
integer.
Because primitive strings in JavaScript expose a .length
property and indexed characters, _.isArrayLike treats them
as array-like.
const _ = require('lodash');
_.isArrayLike([1, 2, 3]); // true
_.isArrayLike(document.querySelectorAll('div')); // true
_.isArrayLike('hello'); // true
_.isArrayLike({ length: 5 }); // true
_.isArrayLike(() => {}); // false
_.isArrayLike(null); // falseHow
_.isArrayLikeObject Works
The _.isArrayLikeObject method combines the logic of
_.isArrayLike with _.isObjectLike. For a value
to pass this check, it must satisfy two conditions simultaneously:
- It must be array-like (possessing a valid
lengthproperty and not being a function). - It must be an object-like entity (where
typeof value === 'object'andvalue !== null).
This eliminates primitive values entirely, even if they possess a
valid .length property.
const _ = require('lodash');
_.isArrayLikeObject([1, 2, 3]); // true
_.isArrayLikeObject(document.querySelectorAll('div')); // true
_.isArrayLikeObject({ length: 5 }); // true
_.isArrayLikeObject('hello'); // false
_.isArrayLikeObject(new String('hello')); // true (String object wrapper)Key Differences at a Glance
The primary divergence between the two methods lies in primitive handling:
- Strings: A primitive string like
'abc'yieldstruewith_.isArrayLike, but yieldsfalsewith_.isArrayLikeObject. - Other Primitives: Numbers, booleans, symbols,
null, andundefinedreturnfalsefor both functions, as they lack an array-likelengthproperty. - Complex Types: Regular arrays, NodeLists, argument
lists, and custom objects with a numeric
lengthproperty returntruefor both methods.
| Value | _.isArrayLike |
_.isArrayLikeObject |
|---|---|---|
[1, 2, 3] |
true |
true |
arguments |
true |
true |
document.body.children |
true |
true |
{ '0': 'a', length: 1 } |
true |
true |
'example string' |
true |
false |
new String('example') |
true |
true |
() => {} |
false |
false |
null /
undefined |
false |
false |
Which One Should You Use?
Choose _.isArrayLike when your
algorithm merely needs to iterate over indexed elements by length and
can safely handle reading characters from strings in the exact same
manner as reading array elements.
Choose _.isArrayLikeObject when your
code expects to mutate the target, assign properties to it, or pass it
to operations that are invalid on primitive data types. Because
primitive strings are immutable in JavaScript, attempting to manipulate
them like an array or an object often leads to silent failures or
runtime errors.