Hiprup

What is the output of typeof [] and typeof null?

Both return "object", which surprises people.

  • typeof [] — "object", because arrays are objects. Detect arrays with Array.isArray().

  • typeof null — "object", a long-standing language bug. Check with value === null.

Takeaway: typeof can't reliably identify arrays or null — use Array.isArray and === null instead.

console.log(typeof []);          // 'object' (array is an object)
console.log(typeof {});          // 'object'
console.log(typeof null);        // 'object' (BUG!)
console.log(typeof undefined);   // 'undefined'
console.log(typeof function(){}); // 'function'
console.log(typeof 42);          // 'number'
console.log(typeof 'hello');     // 'string'
console.log(typeof true);        // 'boolean'
console.log(typeof Symbol());    // 'symbol'
console.log(typeof 42n);         // 'bigint'

// Correct checks
console.log(Array.isArray([]));  // true
console.log(null === null);       // true
console.log([] instanceof Array); // true

typeof cannot distinguish arrays from objects or detect null. Use Array.isArray for arrays, === null for null. typeof correctly identifies: number, string, boolean, undefined, function, symbol, and bigint. typeof null === 'object' is a famous JS bug that cannot be fixed without breaking the web.

typeof null = 'object' is the #1 JS trivia question. Know why (historical bug from JS 1.0). typeof [] = 'object' — use Array.isArray.

These two typeof results catch many candidates off guard.

What is the output of typeof [] and typeof null? | Hiprup