Lodash _.isArray() method
What you’ll learn
- How
_.isArray(value)classifies real Arrays. - Why array-like plain objects and typed arrays usually return
false. - How this lines up with native
Array.isArray. - When to pair with
_.isArrayLike,castArray, ortoArray.
Prerequisites
Comfort with JavaScript Arrays and the idea that some objects expose length without being Arrays.
- You know
[]literals versus plain{ }objects. - You can open Try-it labs in the browser.
Overview
_.isArray is the lodash-branded way to ask “Is this an Array instance?”—useful in pipelines, predicates, and mixed-input utilities where you want the same semantics as Array.isArray but consistent lodash imports.
Strict Array branding
Nested arrays, sparse arrays, and Array subclasses still count when the chain matches.
Not duck typing
{ length: 1, 0: "x" } is array-like but fails _.isArray.
Typed arrays excluded
Uint8Array and friends need separate checks when you care about binary buffers.
Syntax
_.isArray(value) - value: any value to test.
- Returns:
trueifvalueis an Array; otherwisefalse.
Literals, nested arrays, and Array.of
Ordinary Array instances—including nested structures created with literals or Array.of—pass the check.
import isArray from "lodash/isArray";
isArray([]); // true
isArray([1, 2, 3]); // true
isArray([[1]]); // true
isArray(Array.of(10, 20)); // true Objects, strings, and typed arrays
Common impostors—plain objects, strings, and typed arrays—fail unless you normalize them into real Arrays first.
import isArray from "lodash/isArray";
isArray({ 0: "a", length: 1 }); // false (array-like object)
isArray("split me"); // false
isArray(new Uint8Array(2)); // false Rest parameters and Array.from(arguments)
Rest bundles are real Arrays. Converting legacy arguments with Array.from produces an Array as well.
import isArray from "lodash/isArray";
function viaRest(...items) {
return isArray(items);
}
viaRest(1); // true
function viaArguments() {
return isArray(Array.from(arguments));
}
viaArguments(2, 3); // true 📋 _.isArray vs related helpers
| API | What passes |
|---|---|
_.isArray(x) / Array.isArray(x) | Array instances only. |
_.isArrayLike(x) | Objects with finite non-negative length (includes strings, Arguments). |
_.castArray(x) | Wraps non-arrays; leaves Arrays untouched. |
Pitfalls to avoid
NodeList and HTMLCollection
These are array-like DOM collections, not Arrays—convert with Array.from before assuming array methods.
Binary data
Do not use _.isArray to detect Uint8Array or Buffer in Node; use explicit typed-array or Buffer checks.
Deserialized data
JSON.parse('[1,2]') yields an Array; nested plain objects stay objects—verify shape at boundaries.
❓ FAQ
Summary
- Purpose: detect Array instances reliably.
- Aligned with: native
Array.isArrayon typical runtimes. - Next: explore more on Lodash _.isArrayBuffer().
In modern JavaScript engines, Lodash’s _.isArray delegates to Array.isArray: the check is based on whether the value is created with the Array constructor chain, not on whether it has numeric indices and length.
6 people found this page helpful
