typeof Operator
Last updated: 2026-09-09
typeof Operator
typeof returns a string naming the type of its operand — the quickest way to inspect a value. It is a unary operator written as typeof x or typeof(x). Note the result is a string: typeof 42 is "number", not Number itself.
| Category | Operators |
|---|---|
| Kind | Unary operator |
| ES version | ES1 (1997) |
| Operands | One |
| Undeclared names | Yes (returns "undefined" instead of throwing) |
📝 Syntax
typeof operand
typeof(operand)
⚙️ Parameters
| 操作数 | Any value or variable; even an undeclared name is safe and yields "undefined". |
|---|
Returns:A type string: "undefined", "boolean", "number", "bigint", "string", "symbol", "function" or "object".
Mutates the original:No (returns a new value)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
number
string
boolean
undefined
object
object
object
function
⚠️ Warning typeof null returns "object" — a bug frozen into the language since 1995, when null carried the object type tag. To test for null compare directly: x === null.
💡 Tip typeof cannot tell an array from a plain object (both are "object"); use Array.isArray(x) instead.
🌐 Browser support
| Browser | |||||
|---|---|---|---|---|---|
| typeof | ✓ v1 | ✓ v12 | ✓ v1 | ✓ v1 | ✓ v1 |
| Supported by all modern browsers | |||||
🏷️ Related Operators
💡 Related Cases
More case studies coming soon — check back later.
📚 Related Tutorials
❓ FAQ
QWhy is typeof null "object"?
AIt is an artifact of the very first implementation: values were stored in 32 bits with a low type tag, and null is all zeros, which landed in the object tag range. Too much early web code depended on it to fix during standardization, so test null with x === null.
QCan typeof distinguish an array from an object?
ANo — arrays, plain objects, dates and regular expressions all report "object". Use Array.isArray(x), x instanceof Date, or Object.prototype.toString.call(x) to tell them apart.
QHow do I test a variable that may not be declared?
Atypeof someVar !== "undefined" is the only safe form. Writing if (someVar) throws a ReferenceError when the name was never declared, whereas typeof simply returns "undefined" and execution continues.