JavaScript typeof Table
| Expression | Returns | Field note |
|---|---|---|
typeof 42 | 'number' | All numbers are one type - integers and floats alike |
typeof 'text' | 'string' | Quotes do not matter |
typeof true | 'boolean' | Only true and false |
typeof undefined | 'undefined' | The one type with exactly one value |
typeof null | 'object' | THE famous bug - kept forever for web compatibility; use === null |
typeof [1,2] | 'object' | Arrays are objects - use Array.isArray instead |
typeof {a: 1} | 'object' | Plain objects; no 'object' subtype detail |
typeof fn | 'function' | The only behavior typeof distinguishes from object |
typeof undeclared | 'undefined' | Safe on undeclared names - the classic guard idiom |
typeof Symbol() | 'symbol' | ES2015 primitive for unique keys |
typeof 10n | 'bigint' | ES2020 - the n suffix |
typeof is JavaScript's type probe: one operator, eleven possible return strings, and two famous quirks. The table below lists every return with the field note that matters - including the 1995 bug the language can never fix and the one idiom that survives because of it.
Bottom line: typeof reliably separates the primitives (number, string, boolean, undefined, symbol, bigint) from functions - and fails at exactly two jobs everyone tries to give it. typeof null returns 'object' (a historical bug kept forever because fixing it would break the web), and arrays return 'object' too. For those two checks the language offers === null and Array.isArray.
The honest part: typeof's most valuable property is one nobody advertises - it never throws, even on a variable that was never declared. Every other access to an undeclared name throws a ReferenceError; typeof undeclared returns 'undefined' quietly. That is why legacy SDK detection reads if (typeof mySDK !== 'undefined') - and why it survives in modern code despite better alternatives.
How to use
- Match the expression against the table; the Return column is the exact string typeof produces.
- For null and arrays, skip typeof entirely - use === null and Array.isArray, the two checks the table's notes recommend.
- Use the undeclared-safe property only for detection idioms (feature SDKs, globals from other scripts); declared code needs no guard.
Frequently asked questions
Why does typeof null return 'object'?
Historical accident, permanently frozen: in the first JavaScript engine, values were tagged by their low bits, and null - a special pointer - shared the object tag. A fix was proposed for ES6 and withdrawn because it would break untold numbers of sites that (incorrectly) rely on the current behavior. The language is stuck with it forever; the correct null check is === null, which is also what the equality table recommends over any type-based probe.
Why does typeof return 'object' for arrays?
Because arrays ARE objects in JavaScript - Array is a subclass of Object with extra behavior. typeof has no 'array' return value and never will. Use Array.isArray(value) for the check - it is exact, works across iframes (where instanceof Array famously fails because each frame has its own Array constructor), and reads better than the legacy Object.prototype.toString.call fallback.
How is typeof safe on undeclared variables while everything else throws?
typeof is an operator, not a property access: the engine special-cases undeclared identifiers and returns 'undefined' instead of throwing a ReferenceError. Every other access - reading, passing, even just mentioning the name in most contexts - throws. That safety is why the legacy idiom if (typeof myLib !== 'undefined') works for detecting whether another script loaded, and why modern code prefers it in exactly that detection context and nowhere else.
What is the difference between typeof and instanceof?
typeof returns a primitive's type string and works on any expression; instanceof checks the prototype chain against a specific constructor and answers 'was this made by that class?'. They overlap only for objects: typeof gives 'object' for arrays, dates and plain objects alike, while instanceof distinguishes them (date instanceof Date is true). The division of labor: typeof for primitives and undeclared-safe checks, instanceof for the object family - and Array.isArray for the one case both get wrong.