#64: typeof vs Array.isArray
Topic: misc · Difficulty: ★
const arr = [1, 2, 3];
console.log(typeof arr);
console.log(Array.isArray(arr));Output
object true
Why
The typeof operator has no special case for arrays — arrays are just a specialized kind of object under the hood, so typeof arr reports 'object', exactly the same as it would for a plain {} or any other non-function object. This means typeof alone can never distinguish an array from a plain object. Array.isArray(), added specifically to solve this gap, inspects the internal [[Class]] tag of the value rather than relying on typeof, correctly identifying real arrays (including ones created across different execution contexts like iframes, where instanceof Array can fail) and returning true here.