JavaScript typeof
The typeof operator returns a string describing the type of a value. It's the first tool to reach for when you need to check what kind of data you're dealing with — for example, to validate function arguments or handle input that could be a number or a string.
Basic usage
console.log(typeof "hello");
console.log(typeof 42, typeof 3.14, typeof NaN, typeof Infinity);
console.log(typeof 10n);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof Symbol("id"));
console.log(typeof function () {}, typeof class {}, typeof Math.max);
console.log(typeof {}, typeof [], typeof null, typeof new Date(0));string
number number number number
bigint
boolean
undefined
symbol
function function function
object object object object
The complete table
| Value | typeof |
|---|---|
"text", '', `t` |
"string" |
42, NaN, Infinity |
"number" |
42n |
"bigint" |
true, false |
"boolean" |
undefined, undeclared variable |
"undefined" |
Symbol() |
"symbol" |
| functions, classes | "function" |
null |
"object" ⚠️ |
| objects, arrays, dates, regexes, Maps, Sets | "object" |
There are only eight possible results. Note that typeof is an operator, not a function — typeof x and typeof(x) both work, but the parentheses are just grouping.
The quirks
Three results trip people up:
typeof nullis"object"— a bug from 1995 that can never be fixed without breaking the web.typeof []is"object"— arrays are objects.typeof NaNis"number"— "Not a Number" is technically a number value.
So for these cases, use more precise checks:
const values = [null, [1, 2], NaN, {}, new Map()];
for (const v of values) {
console.log(
String(v).padEnd(26),
v === null,
Array.isArray(v),
Number.isNaN(v),
v instanceof Map
);
}null true false false false
1,2 false true false false
NaN false false true false
[object Object] false false false false
[object Map] false false false true
instanceof checks whether a value was created by a particular constructor (class), which works for dates, maps, errors and your own classes. For built-in types there's also a precise "internal tag":
const d = new Date(0);
console.log(typeof d, d instanceof Date, d.toISOString());
console.log(Object.prototype.toString.call(d));
console.log(Object.prototype.toString.call([]));
console.log(Object.prototype.toString.call(null));object true 1970-01-01T00:00:00.000Z
[object Date]
[object Array]
[object Null]
Object.prototype.toString.call(value) gives a precise internal tag for built-in types.
Undeclared variables
typeof is the only operator that doesn't throw for a variable that was never declared — it returns "undefined". This is used to detect features safely:
console.log(typeof someUndeclaredThing);
try {
someUndeclaredThing;
} catch (err) {
console.log(err.message);
}
console.log(typeof structuredClone === "function" ? "structuredClone available" : "missing");undefined
someUndeclaredThing is not defined
structuredClone available
A practical type checker
function describe(value) {
if (value === null) return "null";
if (Array.isArray(value)) return `array of ${value.length}`;
switch (typeof value) {
case "string": return `string "${value}"`;
case "number": return Number.isNaN(value) ? "NaN" : `number ${value}`;
case "function": return `function ${value.name || "(anonymous)"}`;
case "object": return `object with keys ${Object.keys(value).join(", ")}`;
default: return typeof value;
}
}
for (const v of ["hi", 7, NaN, null, [1, 2, 3], { a: 1, b: 2 }, parseInt, true, undefined]) {
console.log(describe(v));
}string "hi"
number 7
NaN
null
array of 3
object with keys a, b
function parseInt
boolean
undefined
Test yourself
What is typeof typeof 42?
- "number"
- "string"
- "typeof"
- "undefined"
What does typeof null return?
- "null"
- "object"
- "undefined"
- "empty"
Which check correctly detects an array?
- typeof x === "array"
- Array.isArray(x)
- x instanceof Object
- typeof x === "object"