Skip to content

TypeOf Operator

typeof is an operator that returns a string naming the type of a value. It is written before the value, without brackets: typeof value. It is mainly used to check what kind of value a function has been given before working with it.

const examples = [
['a string', 'Hello'],
['a number', 3.5],
['NaN', NaN],
['a bigint', 10n],
['a boolean', false],
['undefined', undefined],
['a symbol', Symbol('key')],
['null', null],
['an object', { id: 1 }],
['an array', [1, 2]],
['a date', new Date()],
['a function', () => {}],
['a class', class Shelf {}],
];
for (const [label, value] of examples) {
console.log(`${label}: ${typeof value}`);
}

This prints:

a string: string
a number: number
NaN: number
a bigint: bigint
a boolean: boolean
undefined: undefined
a symbol: symbol
null: object
an object: object
an array: object
a date: object
a function: function
a class: function

So typeof can return only eight strings: "string", "number", "bigint", "boolean", "undefined", "symbol", "object" and "function". Each primitive type has its own result, except null. Every object gives "object", except functions and classes, which give "function".

Three results catch people out:

  • typeof null is "object". This is a bug from the first version of JavaScript, kept because changing it would break existing code.
  • An array is an object, so typeof cannot tell an array from any other object.
  • NaN means “not a number” but has the number type, because it is the result of a failed calculation. NaN and isNaN Operator covers how to test for it.

Use a direct check for the first two:

const shelves = ['fiction', 'history'];
const reading = null;
console.log(Array.isArray(shelves));
console.log(reading === null);
function isPlainObject(value) {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
console.log(isPlainObject({ title: 'Persuasion' }));
console.log(isPlainObject(shelves));
console.log(isPlainObject(reading));

This prints:

true
true
true
false
false

To tell one kind of object from another, such as a Date from a Map, use instanceof.

typeof works well in a switch, where each case handles one type:

function describe(value) {
switch (typeof value) {
case 'number':
return `a number: ${value.toFixed(1)}`;
case 'string':
return `a string of ${value.length} characters`;
default:
return `something else (${typeof value})`;
}
}
console.log(describe(21.456));
console.log(describe('basket'));
console.log(describe(true));

This prints:

a number: 21.5
a string of 6 characters
something else (boolean)

Reading a variable that was never declared throws a ReferenceError, but typeof on such a name returns "undefined" without an error. That makes it a safe way to test whether a global feature exists before using it:

console.log(typeof missingSetting);
if (typeof structuredClone === 'function') {
console.log('structuredClone is available');
}

This prints:

undefined
structuredClone is available

The protection does not cover a let, const or class that is declared later in the same scope, where typeof throws. Hoisting - var vs let shows this case.

Date() called without new returns the current date as a string, not a Date object. And because typeof always returns a string, typeof typeof anything is "string":

console.log(typeof Date());
console.log(typeof new Date());
console.log(typeof typeof 42);

This prints:

string
object
string

Steve Griffith covers this topic in his video JavaScript typeof Operator.