Skip to main content

Checking whether a value is an array

To check whether a value is an array, use Array.isArray(value). A dedicated function is needed because in JavaScript an array is a kind of object, and typeof [] returns 'object', which distinguishes nothing.

Theory

TL;DR

  • Array.isArray(value) is the modern and reliable way, standardised in ES5.
  • It works even when the array was created in another context (an iframe or a separate window).
  • value instanceof Array reads clearly but returns false for an array from a foreign context.
  • Object.prototype.toString.call(value) === '[object Array]' is the old fallback.
  • typeof does not fit this task: it returns 'object' for an array.
  • An array-like object ({ length: 2 }) is not an array, and no check will say otherwise.

Quick example

javascript
console.log(Array.isArray([1, 2, 3])); // true console.log(Array.isArray('hello')); // false console.log(Array.isArray({ length: 0 })); // false console.log(Array.isArray(new Array())); // true console.log(Array.isArray(null)); // false

Way 1: Array.isArray()

javascript
Array.isArray(value);

This is the standard ES5 method created specifically for this check. It returns true when the value is an array and false in every other case.

javascript
Array.isArray([1, 2, 3]); // true Array.isArray([]); // true Array.isArray('text'); // false Array.isArray({ 0: 'a', 1: 'b', length: 2 }); // false Array.isArray(null); // false

The method also works correctly across execution contexts (an iframe, a separate window and so on), because it inspects the value's internal slot rather than its prototype. That is what makes it the only fully reliable way.

Way 2: instanceof Array

javascript
value instanceof Array;

An example:

javascript
console.log([1, 2, 3] instanceof Array); // true console.log({} instanceof Array); // false

The drawback: if the array was created in another window or in an iframe, the check returns false, because that array has a different Array constructor:

javascript
// every context has its own global Array iframe.contentWindow.Array !== window.Array;

instanceof walks the prototype chain, and prototypes from different contexts are different objects. On top of that, the check can be fooled by replacing an object's prototype.

Way 3: Object.prototype.toString

javascript
Object.prototype.toString.call(value) === '[object Array]';

An example:

javascript
console.log(Object.prototype.toString.call([1, 2, 3])); // "[object Array]" console.log(Object.prototype.toString.call({})); // "[object Object]"

It works correctly, including across contexts, but it looks bulky, so these days it is almost always replaced with Array.isArray(). Historically it was the main approach before ES5, and it still shows up in polyfills.

Comparing the approaches

MethodReturnsProsCons
Array.isArray()true / falseModern, reliable, readableNone
instanceof Arraytrue / falseClear syntaxDoes not work across contexts
Object.prototype.toString.call()"[object Array]"Always worksVerbose to write

Common mistakes

  • Checking with typeof. typeof [] gives 'object', exactly like {} and null, so nothing is distinguished.
  • Relying on a length property. A string and arguments have length too, yet neither is an array.
  • Using instanceof on data from an iframe or another window. There the global Array is a different one, and the check silently returns false.
  • Confusing "array-like" with "array". arguments and NodeList are array-like collections: you can iterate them, but they have no array methods. Array.from() converts them.
  • Forgetting that null is 'object' too. An array check cannot be replaced by typeof value === 'object'.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.