Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

typeof: checking a value category

typeof returns a string describing a JavaScript value category. It helps identify primitive types but does not distinguish every kind of object.

Version: ECMAScript 2020+

Syntax

typeof expression

Results include strings such as "number", "string", "boolean", "undefined", "bigint", "symbol", "function", and "object". Compare these strings explicitly, for example with typeof value === "string". NaN still has type number: the type does not prove that a value is suitable for arithmetic.

typeof null historically returns "object". Arrays return "object" too; use Array.isArray to identify them. When rejecting an empty value, check value !== null rather than relying only on typeof.

typeof missingName can return "undefined" for an undeclared name. This does not mean the operator never throws: accessing a let or const variable in its temporal dead zone throws ReferenceError. Reading a missing property also requires a valid base object.

The operator does not change the value. Parentheses clarify which expression is inspected: typeof (1 + "2") checks the addition result, not just the number 1.

Examples

console.log(typeof 5, typeof "5", typeof NaN);
console.log(typeof null, typeof []);
console.log(Array.isArray([]));
console.log(typeof (1 + "2"));

Result

number string number
object object
true
string
try {
  console.log(typeof value);
  let value = 3;
} catch (error) {
  console.log(error.name);
}

Result

ReferenceError

Sources