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 Arrayworks, but depends on the context: an array from an iframe has a differentArray.prototype.Object.prototype.toString.call(value)returns"[object Array]", universal but verbose.typeof valueis 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
{}andnull.
Quick example
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
Array.isArray([1, 2, 3]); // true
Array.isArray('text'); // false
Array.isArray({}); // falseWhy 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
[1, 2, 3] instanceof Array; // true
'abc' instanceof Array; // falseThere 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
Arrayconstructor and its ownArray.prototype.
// 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)); // trueOne 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.
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
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
| Method | Returns for an array | Reliability | Comment |
|---|---|---|---|
Array.isArray(value) | true | Highest | The best option |
value instanceof Array | true | Medium | Depends on the execution context |
Object.prototype.toString.call(value) | "[object Array]" | High | Universal, but verbose |
typeof value | "object" | None | Does 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,DateandMap. - Forgetting
null.typeof nullis"object"as well, so any "is it an object" test has to rule outnullseparately. - Relying on
instanceofin code with iframes, Workers or several contexts. There a genuine array gives youfalse. - Treating array-likes as arrays.
arguments,NodeListand any object with alengthfield are not arrays:Array.isArray()returnsfalsefor them. UseArray.from(value)or[...value]to get a real array. - Checking
value.constructor === Array. Theconstructorproperty is easy to overwrite, and the code throws onnullandundefined.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.