Lodash _.isArrayLike() method
What you’ll learn
- How
_.isArrayLike(value)spots iterable-ish indexed collections. - Why strings and legacy
argumentsqualify while functions do not. - How this differs from
_.isArrayand_.isArrayLikeObject. - When invalid
lengthvalues fail the check.
Prerequisites
Familiarity with length on strings and Arrays, and the difference between Arrays and plain objects.
- You understand why functions have a
lengthproperty (arity). - You can open Try-it labs in the browser.
Overview
_.isArrayLike powers Lodash’s collection methods when inputs might be Arrays, strings, NodeLists, or shaped POJOs. Use it when you care about indexability plus length—not strict Array typing.
Beyond Arrays
Strings, arguments, typed arrays, and custom list-like objects can qualify.
Functions excluded
Arity length does not make a callable look like a list.
Validated length
NaN, negative, or infinite lengths fail—Lodash mirrors safe length rules.
Syntax
_.isArrayLike(value) - value: any value to test.
- Returns:
trueifvaluelooks like an indexed collection with a valid length; otherwisefalse.
Arrays, strings, and classic arguments
Dense Arrays qualify immediately; strings expose UTF-16 length; non-arrow functions still expose arguments with both indices and length.
import isArrayLike from "lodash/isArrayLike";
isArrayLike([10, 20]); // true
isArrayLike("hi"); // true
function demo() {
return isArrayLike(arguments);
}
demo(1, 2, 3); // true Shaped objects versus functions
Plain objects can mimic lists if length is valid. Functions are rejected even though they carry length.
import isArrayLike from "lodash/isArrayLike";
isArrayLike({ 0: "x", length: 1 }); // true
isArrayLike(function (a, b) {}); // false (arity length ignored)
isArrayLike(null); // false Typed arrays, Maps, and bad lengths
Typed arrays are collections with element counts. Maps expose size, not length, so they fail. Invalid lengths fail even when indices exist.
import isArrayLike from "lodash/isArrayLike";
isArrayLike(new Uint8Array(4)); // true
isArrayLike(new Map()); // false
isArrayLike({ 0: 1, length: -1 }); // false 📋 _.isArrayLike vs nearby helpers
| API | Typical match |
|---|---|
_.isArrayLike(x) | Arrays, strings, arguments, typed arrays, list-shaped objects. |
_.isArray(x) | True Arrays only. |
_.isArrayLikeObject(x) | Array-like objects (excludes string primitives). |
_.isTypedArray(x) | Typed arrays specifically. |
Pitfalls to avoid
String length vs glyphs
String length counts UTF-16 code units; combining characters and surrogate pairs need separate handling for human-visible counts.
Need real Arrays?
Array-like DOM collections pass _.isArrayLike but still lack Array prototypes until you call Array.from.
Holes and exotic objects
Array-like checks do not guarantee dense data—always validate business rules separately.
❓ FAQ
Summary
- Purpose: detect collection-like values with a sane
length. - Not: a substitute for
Array.isArraywhen you require Array methods. - Next: explore more on Lodash _.isArrayLikeObject().
_.isArrayLike implements Lodash’s shared notion of a finite, non-negative length that behaves like indexed collections—it purposely excludes functions (which also have a length for arity) so arity does not look like a list.
6 people found this page helpful
