Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The typeof operator is a unary operator that evaluates its operand and returns a string value representing the underlying JavaScript data type of that operand.

Syntax

The operator can be applied with or without grouping parentheses. The parentheses act as standard mathematical grouping operators, not as function invocation.
typeof operand
typeof (operand)

Return Values

The ECMAScript specification restricts the typeof operator to returning one of eight specific string values.
Operand TypeReturn Value
Undefined"undefined"
Null"object"
Boolean"boolean"
Number"number"
BigInt"bigint"
String"string"
Symbol"symbol"
Function (implements [[Call]])"function"
Any other Object"object"

Syntax Visualization

// Primitives
typeof undefined;           // "undefined"
typeof true;                // "boolean"
typeof 42;                  // "number"
typeof 10n;                 // "bigint"
typeof "ECMAScript";        // "string"
typeof Symbol("ref");       // "symbol"

// Objects
typeof { key: "value" };    // "object"
typeof [1, 2, 3];           // "object"
typeof new Date();          // "object"
typeof new String("text");  // "object" (Wrapper object, not primitive)

// Functions
typeof function() {};       // "function"
typeof class C {};          // "function"
typeof Math.sin;            // "function"

Technical Quirks and Edge Cases

The null Bug
typeof null; // "object"
This is a recognized historical artifact in JavaScript. In the first implementation of JS, values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (which is 0x00 on most platforms). Consequently, null had an object type tag, causing typeof to return "object". This behavior is permanently retained in the ECMAScript specification for backward compatibility. Not-A-Number (NaN)
typeof NaN; // "number"
Despite its name, NaN is a numeric data type defined by the IEEE 754 floating-point standard. Therefore, the engine evaluates it as a number. Undeclared Variables vs. Temporal Dead Zone (TDZ) Applying typeof to an undeclared variable is the only way to safely read or evaluate it without throwing a ReferenceError. It safely returns "undefined".
typeof undeclaredVariable; // "undefined"
However, this safety mechanism does not apply to variables declared with let or const that are currently in the Temporal Dead Zone (TDZ). Attempting to evaluate them before initialization will throw an error.
typeof blockScopedVar; // ReferenceError: Cannot access 'blockScopedVar' before initialization
let blockScopedVar = 10;
Document.all
typeof document.all; // "undefined"
document.all is a host object that implements a special [[IsHTMLDDA]] internal slot. The ECMAScript specification explicitly dictates that typeof must return "undefined" for objects implementing this slot, primarily to support legacy browser detection code.
Master JavaScript with Deep Grasping Methodology!Learn More