typeof in JavaScript: reading the type of a value, and its limits

The typeof operator returns the type of a value as text. The eight possible answers, the two well-known traps, and the tests you should use instead.
3 min read
Believemy logo

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.

JAVASCRIPT
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 knowThe right test
Is it a string, a number, a booleantypeof value === 'string'
Is it nullvalue === null
Is it an arrayArray.isArray(value)
Is it an instance of a classvalue 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.

JAVASCRIPT
console.log(typeof missingVariable); // 'undefined'
console.log(missingVariable);        // ReferenceError

That 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.

Good to know

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

Question

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.


Question

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.


Question

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.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.