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.
What typeof returns
Section titled “What typeof returns”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: stringa number: numberNaN: numbera bigint: biginta boolean: booleanundefined: undefineda symbol: symbolnull: objectan object: objectan array: objecta date: objecta function: functiona class: functionSo 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".
Where typeof misleads
Section titled “Where typeof misleads”Three results catch people out:
typeof nullis"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
typeofcannot tell an array from any other object. NaNmeans “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:
truetruetruefalsefalseTo tell one kind of object from another, such as a Date from a Map, use instanceof.
Checking a value’s type
Section titled “Checking a value’s type”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.5a string of 6 characterssomething else (boolean)Checking whether something exists
Section titled “Checking whether something exists”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:
undefinedstructuredClone is availableThe 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.
Two small surprises
Section titled “Two small surprises”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:
stringobjectstringSteve Griffith covers this topic in his video JavaScript typeof Operator.