Hiprup

How do you check if a value is an array?

The reliable way is Array.isArray(value).

  • Array.isArray() — returns true only for real arrays; the recommended method.

  • Why not typeof — typeof [] is "object", so it can't identify arrays.

Bonus: Array.isArray even works across iframes/realms, where instanceof Array can fail.

console.log(Array.isArray([]));          // true
console.log(Array.isArray([1, 2, 3]));   // true
console.log(Array.isArray(new Array())); // true

console.log(Array.isArray({}));          // false
console.log(Array.isArray('hello'));     // false
console.log(Array.isArray(123));         // false
console.log(Array.isArray(null));        // false
console.log(Array.isArray(undefined));   // false

// Why not typeof?
console.log(typeof []);     // 'object' — not helpful!
console.log(typeof {});     // 'object' — same!
console.log(typeof null);   // 'object' — also same!

// Why not instanceof?
// [] instanceof Array works normally but fails across iframes
// (different Array constructor in different realms)

Array.isArray is the only reliable method. typeof returns 'object' for arrays, objects, and null (useless). instanceof fails across iframes/windows because each has its own Array constructor. Array.isArray works correctly in all scenarios.

Array.isArray is the answer — period. typeof [] = 'object' (useless). instanceof fails across iframes. This is a quick question but shows awareness of type-checking pitfalls.

How do you check if a value is an array? | Hiprup