typeof
Returns a string with the name of a value’s type, e.g. "number", "string", or "object".
- Returns
- A string with the type name:
"undefined","boolean","number","bigint","string","symbol","function", or"object".
Example
#console.log(typeof 42)
console.log(typeof 'Emma')
console.log(typeof true)
console.log(typeof undefined)
console.log(typeof { level: 1 })
console.log(typeof (() => {}))Definition and usage
#The typeof operator checks what type a value is and returns the answer as a string. The possible results are: "undefined", "boolean", "number", "bigint", "string", "symbol", "function", and "object".
It’s useful for checking function arguments and data from the outside, like whether a user actually provided a number. You can even use it on an undeclared variable: it then returns "undefined" instead of an error, which is why it’s used to check whether some API exists in the environment.
It has two well-known pitfalls. typeof null returns "object", which is a historical bug in the language. Arrays are also "object", so Array.isArray() is what you use to recognize them. It’s also worth remembering that NaN has the type "number".
Syntax
#typeof operand
typeof (expression)More examples
#console.log(typeof null)
console.log(typeof [1, 2, 3])
console.log(Array.isArray([1, 2, 3]))
console.log(typeof NaN)function addPoints(current, amount) {
if (typeof amount !== 'number' || Number.isNaN(amount)) {
return current
}
return current + amount
}
console.log(addPoints(100, 20))
console.log(addPoints(100, '20'))console.log(typeof notDeclared)
if (typeof structuredClone === 'function') {
console.log('structuredClone() is available')
}A plain read of an undeclared variable throws a ReferenceError, while typeof returns "undefined" instead. The exception is let and const variables used before their declaration: there you still get an error.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- To check for
null, usevalue === null, and for arrays,Array.isArray(value).typeofreturns"object"for both. - You recognize class instances, like
Dateor your own, with theinstanceofoperator.typeofsimply returns"object"for them. - Mind the precedence:
typeof a + bis(typeof a) + b. To check the type of an expression’s result, add parentheses:typeof (a + b).
Related entries
#- ===The strict equality operator compares values without type conversion: true only for the same type and the same value.
- Array.isArray()Checks whether a value is an array.
- Number.isNaN()Checks whether a value is exactly NaN.
- ?.The ?. (optional chaining) operator safely reads a property. When the object is null or undefined, it returns undefined instead of throwing.
- instanceofChecks whether an object was created by a given class, or by a class that inherits from it.
- SymbolA primitive type for creating unique identifiers, such as property keys that never collide with others.
Spotted a mistake or missing an example? Write to us.