typeof null and Other Gotchas
JS · Common Mistakessyntax
typeof null === "object" // true (historic bug)
typeof NaN === "number" // true (NaN is a number type)
typeof [] === "object" // true (arrays are objects)example
// All of these are surprising but correct
console.log(typeof null); // "object"
console.log(typeof NaN); // "number"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"
// Better checks
console.log(value === null); // null check
console.log(Number.isNaN(value)); // NaN check
console.log(Array.isArray(value)); // array checkNote typeof is unreliable for null, arrays, and NaN. Use specialized checks: === null, Array.isArray(), Number.isNaN(), or instanceof for specific types.