Code Notes

Check if value is array in JavaScript

Use Array.isArray(). That’s it — it handles every case, including values from other realms.

Array.isArray([]) // true
Array.isArray('test') // false
Array.isArray({}) // false

Why not the alternatives?

instanceof Array

;[] instanceof Array // true
'test' instanceof Array // false

Fails across realms: an array created in an iframe or worker has a different Array constructor, so instanceof returns false for a perfectly good array.

.constructor === Array

;[].constructor === Array // true
'foo'.constructor === Array // false

Throws on null/undefined, has the same cross-realm problem, and constructor can be reassigned so it’s spoofable anyway.

From Stack Overflow