JavaScript typeof: Reading Runtime Type Tags
Quick answer
typeof returns a string label for an operand. It is helpful for many primitive values, but it is not a complete classifier for objects and it has historical behavior for null.
How it works
The labels for ordinary values include undefined, boolean, number, bigint, string, symbol, object and function. Null returns "object" despite being a separate primitive type. Callable objects return "function". An unresolvable bare identifier usually gives "undefined" with typeof, while a name in the TDZ still throws ReferenceError. Browser document.all is a legacy exception.
console.log(typeof 8);console.log(typeof "8");console.log(typeof null);Expected output in Node.js:
numberstringobjectNull is the historical exception to direct type labeling.
function task() {}console.log(typeof task);console.log(typeof {});Expected output in Node.js:
functionobjectA callable ordinary function is an object with a special typeof tag.
Common mistakes
To recognize null use value === null, not typeof value === "null". Arrays and ordinary objects both produce object; a later chapter introduces Array.isArray. Do not use typeof to bypass TDZ.
Practice
Return the exact type tag of an input value as a string.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript typeof: Reading Runtime Type Tags — Knowledge Check
Apply the taught concept and reason about the result.
What does typeof null return?
What does typeof 12n return?
What does function task() {} console.log(typeof task); print?
Which expression directly tests whether value is null?
What happens in { console.log(typeof later); let later = 1; }?