typeof
Returns a string with the type of a value.
Available since: ES1
Syntax
typeof valueExamples
The types of basic values:
console.log(typeof 42);
console.log(typeof 'hi');
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof (() => {}));Output:
number
string
boolean
undefined
functionSurprising results:
console.log(typeof null);
console.log(typeof []);
console.log(typeof NaN);Output:
object
object
numberNotes
typeof null returns object — an old language bug that can never be fixed. Arrays also give object, so check arrays with Array.isArray() and null with value === null.
The possible answers are undefined, boolean, number, bigint, string, symbol, function and object.
See also
- instanceof — Checks whether an object was created by a class (or a subclass).
- Array.isArray — Checks whether a value is an array.
- Number.isNaN — Checks that a value is NaN, without type conversion.
Related recipes
In the tutorial
- Data types — The main JavaScript data types — numbers, strings, booleans, null and undefined, and the typeof operator.