The typeof Operator
Introduction
typeof is a unary operator that returns a string describing the broad runtime type of a value. It is useful for primitives and safe checks for undeclared names, but it has famous quirks: typeof null is 'object', arrays are 'object', and functions are 'function'.
Why This Matters
typeof appears in almost every JavaScript interview because it tests both practical type checking and historical knowledge. The safest candidates know what typeof can answer, what it cannot, and why typeof null === 'object' should not be used to detect objects blindly.
Theory
Return values
typeof value always returns one of a small set of strings. It does not return constructor names like Array or Date.
| Expression | Result | Notes |
|---|---|---|
typeof undefined | 'undefined' | Also works for undeclared names. |
typeof true | 'boolean' | Boolean primitive. |
typeof 123 / typeof NaN | 'number' | NaN is still a number value. |
typeof 'x' | 'string' | String primitive. |
typeof 10n | 'bigint' | BigInt primitive. |
typeof Symbol('x') | 'symbol' | Symbol primitive. |
typeof function () {} | 'function' | Special callable object result. |
typeof {} / typeof [] / typeof null | 'object' | null is the historical bug. |
The null bug
typeof null returns 'object' because of an early implementation tag bug that became web-compatible behaviour. It cannot be fixed without breaking old code. Always check value !== null when using typeof value === 'object'.
Safe undeclared checks
typeof maybeMissing returns 'undefined' even if maybeMissing was never declared. Directly reading maybeMissing would throw ReferenceError. This is useful for feature detection.
What typeof cannot do
typeof cannot distinguish arrays, dates, maps, sets, regexps, or plain objects. Use Array.isArray(value), value instanceof Date, or more precise checks depending on the problem.
Visual Diagrams
primitive? -> string such as number, string, boolean, bigint, symbol, undefined function? -> function null? -> object (historical bug) object? -> object (arrays, dates, plain objects)
`typeof` is broad; use specialised checks for object subtypes.
Code Examples
Safe object check
Always exclude null when using typeof value === 'object'.
Feature detection with typeof
typeof can safely test undeclared globals without throwing.
Output Prediction
Predict the output #1
1console.log(typeof null);2console.log(typeof function () {});3console.log(typeof NaN);4console.log(typeof []);5console.log(typeof notDeclaredYet);Interview Questions
1Why does `typeof null` return `'object'`?
It is a historical implementation bug from early JavaScript. Values were represented with type tags, and the null pointer matched the object tag pattern. The result became web-compatible behaviour, so it remains. In real checks, use value !== null && typeof value === 'object'.
Follow-ups
- How do you check for arrays?
- Can `typeof` check an undeclared variable safely?
Quiz
1. What is the result of `typeof NaN`?
2. Which is the safest plain object-like guard among these?
Summary
- `typeof` returns broad type strings such as `number`, `string`, `undefined`, `object`, and `function`.
- `typeof null` is `'object'`, a historical bug you must guard against.
- `typeof function () {}` is `'function'`; arrays are still `'object'`.
- `typeof undeclaredName` safely returns `'undefined'` instead of throwing.
Cheat Sheet
Results: undefined, boolean, number, string, bigint, symbol, function, object.
Quirks: typeof null === 'object'; typeof [] === 'object'; typeof NaN === 'number'.
Safe undeclared: typeof maybeGlobal === 'undefined'.
Better checks: Array.isArray, value instanceof Date, value !== null && typeof value === 'object'.