Types & Coercionmedium

typeof & Type Checking Quirks

typeof returns a string representing the type of the operand. The 8 possible returns are: 'undefined', 'boolean', 'number', 'bigint', 'string', 'symbol', 'function', 'object'.

Memory anchor

typeof null === 'object' is JavaScript's original sin from 1995 — a bug so old it has a pension. Think of typeof as a label gun that mislabeled null at the factory and nobody ever recalled it.

Expected depth

typeof null === 'object' is a historical bug from the original JavaScript engine (1995) — null's internal type tag was 0, the same tag used for object references. To check for null: value === null. typeof undeclaredVariable is 'undefined' and does NOT throw — this was useful before let/const for feature detection. typeof [] is 'object' — use Array.isArray() for arrays. typeof function(){} is 'function' even though functions are objects. instanceof checks the prototype chain: [] instanceof Array is true, but fails across frames (different window objects have different Array constructors).

Deep — senior internals

The spec defines 7 language types: Undefined, Null, Boolean, Number, BigInt, String, Symbol, and Object (functions are callable objects). typeof maps these to 8 string values (Null returns 'object' as the bug). For robust type checking: null check (=== null), Array.isArray(), instanceof for class hierarchies, Object.prototype.toString.call(value) for granular type tags ('[object Date]', '[object RegExp]', etc.). Symbol.toStringTag allows objects to customize their toString tag. In TypeScript, you can use type predicates (value is Type) for narrowing.

🎤Interview-ready answer

typeof is safe (never throws, even on undeclared variables) but has the famous null bug: typeof null === 'object'. Use === null for null checks, Array.isArray() for arrays, instanceof for class instances. For comprehensive type identification, Object.prototype.toString.call(value) returns reliable '[object Type]' strings that can't be spoofed without Symbol.toStringTag.

Common trap

typeof in the TDZ does NOT save you for let/const — typeof undeclaredLetVar throws ReferenceError during the TDZ. The safety of typeof for undeclared variables only applies to truly undeclared names (not in the current or enclosing scope at all), not to names declared with let/const that haven't been initialized yet.