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:

  1. It is not a function.
  2. It has a length property that is a non-negative integer less than or equal to Number.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); // false

How _.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:

  1. It must be array-like (possessing a valid length property and not being a function).
  2. It must be an object-like entity (where typeof value === 'object' and value !== 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:

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.