Skip to main content
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.

Return Values

The ECMAScript specification restricts the typeof operator to returning one of eight specific string values.

Syntax Visualization

Technical Quirks and Edge Cases

The null Bug
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)
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".
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.
Document.all
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.
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More