Featureintro
The typeof operator
Also called typeof, typeof null, typeof operator.
typeof x gives a string that names the type of x: "undefined", "boolean", "number", "bigint", "string", "symbol", "function" or "object". typeof null is "object".
- Language
- JavaScript
Synopsis
typeof operand
| Operand | Result |
|---|---|
undefined, an undeclared name |
'undefined' |
null |
'object' |
| a Boolean, number, BigInt, string or symbol | 'boolean', 'number', 'bigint', 'string', 'symbol' |
| a function or class | 'function' |
| any other object | 'object' |
"The typeof operator returns a string indicating the type of the operand's value."1 MDN says of its table of results: "This list of values is exhaustive."1
console.log(typeof undefined, typeof null, typeof true, typeof 1, typeof 1n);
console.log(typeof "s", typeof Symbol(), typeof function () {}, typeof class {}, typeof []);
undefined object boolean number bigint
string symbol function function object
Semantics
r1. The result is a string. The specification returns one of the strings in the table, from the value of the operand.2 So typeof typeof 1 is 'string'.1
r2. typeof null is 'object'. In the specification, the step for a null value returns 'object'.2 MDN explains that "In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value." The tag for objects was 0, and null was the NULL pointer, so it had the same tag.1 Example: synopsis.
r3. Callable objects give 'function'. An object with a [[Call]] internal method gives 'function'.2 Classes are functions too.1 All other objects, which include arrays, dates and regular expressions, give 'object'.1
r4. An undeclared name is OK. MDN says that typeof "works with undeclared identifiers", and returns 'undefined' without an error.1 In the specification, an unresolvable reference gives 'undefined'.2 Example: undeclared.
r5. The temporal dead zone still throws. "using typeof on lexical declarations (let const, using await using, and class) in the same block before the place of declaration will throw a ReferenceError."1 Example: tdz.
r6. Precedence. "The typeof operator has higher precedence than binary operators like addition (+)."1 Example: precedence.
Examples
The examples run with Node.js 24.
A test for a global name that may not exist (rule r4):
console.log(typeof notDeclaredAnywhere);
if (typeof notDeclaredAnywhere === "undefined") console.log("not there");
undefined
not there
The temporal dead zone (rule r5):
This code raises ReferenceError, on purpose.
{
console.log(typeof later);
let later = 1;
}
Use parentheses around an expression (rule r6):
const n = 99;
console.log(typeof n + " foo");
console.log(typeof (n + " foo"));
number foo
string
A check that tells null, arrays and objects apart:
function kind(v) {
if (v === null) return "null";
if (Array.isArray(v)) return "array";
return typeof v;
}
console.log([null, [], {}, new String("s"), "s"].map(kind).join(" "));
null array object object string
Pitfalls
null is not an object
typeof v === 'object' is true for null (rule r2). Test v !== null too.
Wrapper objects
typeof new String('abc') is 'object'. MDN calls the wrapper constructors with new "confusing, dangerous, and wasteful".1 See Primitive wrapper objects.
Arrays
typeof [1, 2, 4] is 'object'. MDN says to "use Array.isArray or Object.prototype.toString.call" to tell arrays from other objects.1 Example: kind.
Comparison
| Topic | JavaScript typeof |
Python type() |
|---|---|---|
| result | a string from a fixed list1 | "a type object"3 |
| subclasses | not shown; most objects give 'object'1 |
isinstance() "takes subclasses into account"3 |
Python type(x) with one argument returns "the type of an object".3 JavaScript typeof gives only a small set of names, so code uses Array.isArray or instanceof to learn more.1 See Objects, values and types and Instance and subclass checks.
References
- typeof, MDN, Description; typeof null; Need for parentheses in syntax; Interaction with undeclared and uninitialized variables
- ECMAScript 2026 Language Specification, 13.5.3.1 Runtime Semantics, Evaluation
- Built-in Functions, The Python Standard Library, type
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 81R1VV Edit this page History Report an error Markdown