Array.isArray
Checks whether a value is an array.
Available since: ES5
Syntax
Array.isArray(value)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | any | The value to check. |
Return value
booleantrue if the value is an array.
Examples
An array and a non-array:
console.log(Array.isArray([1, 2]));
console.log(Array.isArray('12'));Output:
true
falseWhy typeof does not help:
console.log(typeof []);
console.log(typeof {});Output:
object
objectNotes
The typeof operator returns object for an array, just like for a plain object. Only Array.isArray() reliably tells them apart.
See also
- typeof — Returns a string with the type of a value.
- instanceof — Checks whether an object was created by a class (or a subclass).
Related recipes
In the tutorial
- Arrays — What an array is in JavaScript: creating one, indexes, length, changing elements and nested arrays.