Skip to main content

Checking whether a value is an array

To check that a value is an array, use Array.isArray(value): it is the modern and most reliable approach, and it works even for arrays that come from another execution context. The other techniques (instanceof, Object.prototype.toString.call(), typeof) either have limitations or fail to tell an array from a plain object.

Theory

TL;DR

  • Array.isArray(value) is the recommended check; it works everywhere and across contexts.
  • value instanceof Array works, but depends on the context: an array from an iframe has a different Array.prototype.
  • Object.prototype.toString.call(value) returns "[object Array]", universal but verbose.
  • typeof value is always "object" for an array, so it is useless as a check.
  • An array is an object, so any "is it an object?" test also matches {} and null.

Quick example

javascript
const value = [1, 2, 3]; console.log(Array.isArray(value)); // true console.log(value instanceof Array); // true console.log(Object.prototype.toString.call(value)); // "[object Array]" console.log(typeof value); // "object"

The first three checks answer the question; the fourth does not.

Array.isArray, the modern and reliable way

javascript
Array.isArray([1, 2, 3]); // true Array.isArray('text'); // false Array.isArray({}); // false

Why this one:

  • It works in every modern browser and in Node.js.
  • It identifies arrays correctly even from other contexts, for example from an iframe, a Web Worker or a separate Node.js context.
  • It does not depend on the prototype: the method inspects an internal slot of the value, not the __proto__ chain.
  • It is the recommended way to check.

instanceof Array, old but working

javascript
[1, 2, 3] instanceof Array; // true 'abc' instanceof Array; // false

There is a caveat, though:

  • If the array was created in another window or frame, the result may be wrong, because every context has its own Array constructor and its own Array.prototype.
javascript
// The problem in practice: const foreignArray = new window.frames[0].Array(1, 2, 3); console.log(foreignArray instanceof Array); // false console.log(Array.isArray(foreignArray)); // true

One more detail: instanceof can be fooled through Symbol.hasInstance or by swapping the prototype, while Array.isArray() is immune to that.

Checking with Object.prototype.toString.call()

This approach is universal for any type, which is why libraries often use it.

javascript
Object.prototype.toString.call([]); // "[object Array]" Object.prototype.toString.call({}); // "[object Object]" Object.prototype.toString.call('hi'); // "[object String]"

It is also independent of the execution context, but it returns a string you have to compare by hand, so the code ends up longer than Array.isArray().

Why not typeof

javascript
typeof []; // "object"

typeof does not fit, because an array is an object and the result is always "object". The same string comes back for {}, null, new Date() and new Map(), so the check tells you nothing useful about arrays.

Comparing the methods

MethodReturns for an arrayReliabilityComment
Array.isArray(value)trueHighestThe best option
value instanceof ArraytrueMediumDepends on the execution context
Object.prototype.toString.call(value)"[object Array]"HighUniversal, but verbose
typeof value"object"NoneDoes not distinguish arrays from other objects

Conclusion: always use Array.isArray(value), it is the most accurate and the shortest check.

Common mistakes

  • Testing for an array with typeof value === 'object'. The condition is also true for {}, null, Date and Map.
  • Forgetting null. typeof null is "object" as well, so any "is it an object" test has to rule out null separately.
  • Relying on instanceof in code with iframes, Workers or several contexts. There a genuine array gives you false.
  • Treating array-likes as arrays. arguments, NodeList and any object with a length field are not arrays: Array.isArray() returns false for them. Use Array.from(value) or [...value] to get a real array.
  • Checking value.constructor === Array. The constructor property is easy to overwrite, and the code throws on null and undefined.

Short Answer

Interview ready
Premium

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