JavaScript lets a variable change type along the way. Data arriving from a form, an API or local storage can be a number, a string, or anything else, and the code receiving it often needs to know before doing anything with it.
typeof answers that question in one word. It is simple, fast, available everywhere, and it holds two surprises worth knowing about before you rely on it.
Definition
typeof is an operator returning, as a string, the type of the value on its right. It changes nothing and works on any value.
typeof 42; // 'number'
typeof 'Believemy'; // 'string'
typeof true; // 'boolean'
typeof undefined; // 'undefined'
typeof Symbol(); // 'symbol'
typeof 10n; // 'bigint'
typeof function () {}; // 'function'
typeof { a: 1 }; // 'object'Since the result is text, the comparison happens against a string, and Strict equality (===) is the rule here: typeof value === 'string'.
The two traps
typeof null returns 'object'. This is a flaw from the earliest versions of the language, kept ever since for compatibility: fixing it would break a considerable number of live websites. To recognize null, compare directly with value === null.
An array is an object. typeof [1, 2] also gives 'object', just like a date, a regular expression or a plain object. The dedicated test exists and costs nothing: Array.isArray(value).
| What you want to know | The right test |
|---|---|
| Is it a string, a number, a boolean | typeof value === 'string' |
| Is it null | value === null |
| Is it an array | Array.isArray(value) |
| Is it an instance of a class | value instanceof Customer |
The one operator that tolerates the unknown
Reading a variable that was never declared raises an error and stops the script. typeof is the single exception: applied to an unknown name, it returns 'undefined' without breaking anything.
console.log(typeof missingVariable); // 'undefined'
console.log(missingVariable); // ReferenceErrorThat tolerance was once used to test for a browser feature. It has a limit: a variable declared with let or const further down the same block does raise an error, because the name already exists for the engine while not yet being usable.
typeof separates basic types, not shapes of objects. To check that a value really is a customer, an error or a date, you need instanceof, or a test for a property with in.
Frequently asked questions
Why does typeof NaN return 'number'?
Because NaN genuinely belongs to the number type: it is the value produced by a numeric calculation that leads nowhere, such as 0 / 0. The test to use in that case is Number.isNaN(value), which answers a different question from the one about type.
Should typeof be written with parentheses?
Both forms work, typeof value as well as typeof(value), because it is an operator rather than a function. The form without parentheses is the most common, and redundant parentheses sometimes make it look like a function call, which does not help anyone reading the code.
How do I tell a plain object from an array or a date?
Start by ruling out null, then use Array.isArray() for arrays and instanceof for dates. If you need a finer answer, Object.prototype.toString.call(value) returns a precise label such as [object Date], at the cost of noticeably worse readability.