TExcelTAD
Home › JavaScript › JS typeof

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

js · try it yourself
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:

  1. typeof null is "object" — a bug from 1995 that can never be fixed without breaking the web.
  2. typeof [] is "object" — arrays are objects.
  3. typeof NaN is "number" — "Not a Number" is technically a number value.

So for these cases, use more precise checks:

js · try it yourself
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":

js · try it yourself
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:

js · try it yourself
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

js · try it yourself
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?

  1. "number"
  2. "string"
  3. "typeof"
  4. "undefined"

What does typeof null return?

  1. "null"
  2. "object"
  3. "undefined"
  4. "empty"

Which check correctly detects an array?

  1. typeof x === "array"
  2. Array.isArray(x)
  3. x instanceof Object
  4. typeof x === "object"