Array.isArray()
Determines whether a value is an array, reliably and across realms.
Array.isArray(value)- Added in JavaScript ES5
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | any | The value to be checked. |
Return value
booleantrue if value is an Array; otherwise false. It never coerces its argument, and false is always returned for a TypedArray instance. It throws a TypeError in exactly one case: when value is a revoked Proxy.
Changelog
- PHP ES2015Respecified as a branded check on the internal array exotic-object slot rather than a prototype inspection, which is what makes it cross-realm correct and immune to prototype spoofing.
typeof [] is "object", so the language gives you no operator that answers this question. Array.isArray() is the answer, and it is the only correct one.
The obvious alternative, value instanceof Array, walks the prototype chain and therefore gets two categories of answer wrong. It reports false negatives for arrays created in another realm — an iframe, a worker, a Node vm context — because that realm has its own Array constructor. And it reports false positives for anything that merely inherits from Array.prototype, such as { __proto__: Array.prototype }. Array.isArray() ignores prototypes entirely and checks an internal brand set at creation, so neither case fools it.
Where the line falls
It answers a narrow question: is this an array exotic object? Typed arrays such as Int16Array are false. So are strings, arguments, NodeList, and any { length, 0, 1 } array-like — all iterable or indexable, none of them arrays. Subclasses of Array are true.
Use it at the boundary of a function that accepts either one item or a list, and before applying array methods to data that came from JSON.parse(), where typeof cannot distinguish [] from {}.
Example
When to use it
- Normalize a parameter that may be a single value or a list before iterating
- Distinguish [] from {} in data returned by JSON.parse()
- Recurse correctly when walking an arbitrary nested structure
- Validate array input at an API boundary where instanceof would break across realms
Frequently asked questions
How do I check if a variable is an array in JavaScript?
Use Array.isArray(value), which returns true only for real arrays. typeof returns "object" for arrays, plain objects and null, so it cannot answer the question.
Why use Array.isArray() instead of instanceof Array?
Because instanceof inspects the prototype chain and gets two cases wrong: it returns false for arrays from another realm such as an iframe, and true for any object whose prototype is Array.prototype. Array.isArray() checks an internal brand, so it is correct in both cases.
Does Array.isArray() return true for typed arrays or array-like objects?
No. Int16Array and other typed arrays, the arguments object, strings, NodeList and any plain { length, 0, 1 } object all return false. Only array exotic objects and subclasses of Array return true.
More examples
Why not instanceof
The spoofed object passes instanceof but fails isArray, because isArray checks an internal brand instead of the prototype chain. The subclass passes both. The last line shows why the question needs a dedicated function: typeof reports "object" for arrays, plain objects and null alike.
const fake = { __proto__: Array.prototype };
console.log(fake instanceof Array, Array.isArray(fake));
class List extends Array {}
const list = List.of(1, 2);
console.log(list instanceof Array, Array.isArray(list));
// typeof is no help at all
console.log(typeof [], typeof {}, typeof null);Normalizing one-or-many input
toList() returns an array in all three cases while leaving an existing array untouched. The final line shows the failure mode of using Array.from() for this job: a string is iterable, so it would be split into characters instead of wrapped.
const toList = (value) =>
value === undefined ? [] : Array.isArray(value) ? value : [value];
console.log(toList("tag"));
console.log(toList(["a", "b"]));
console.log(toList(undefined));
// wrapping without the check would flatten a string into characters
console.log(Array.from("tag"));Walking parsed JSON
The recursion needs three branches, and only isArray() can separate the array branch from the object branch — after JSON.parse() both report "object" under typeof. Output is 4: the numbers 1, 2, 3 and 4.
const data = JSON.parse('{"a":[1,{"b":[2,3]}],"c":4}');
function countNumbers(node) {
if (typeof node === "number") return 1;
if (Array.isArray(node)) return node.reduce((n, v) => n + countNumbers(v), 0);
if (node && typeof node === "object")
return Object.values(node).reduce((n, v) => n + countNumbers(v), 0);
return 0;
}
console.log(countNumbers(data));
Discussion