The typeof operator
suggest changeThe typeof operator evaluates its operand and returns a string describing its type:
typeof operand
| Value category | Result |
|---|---|
undefined or an undeclared identifier |
"undefined" |
| Boolean | "boolean" |
| BigInt | "bigint" |
Number, including NaN and infinities |
"number" |
| String | "string" |
| Symbol | "symbol" |
| Callable object, including a class | "function" |
Any other object, including null |
"object" |
The result for null is a historical language behavior. Test it directly with value === null. Use Array.isArray(value) for arrays because typeof [] is "object".
typeof 3.14; // "number"
typeof NaN; // "number"
typeof 1n; // "bigint"
typeof "text"; // "string"
typeof true; // "boolean"
typeof Symbol(); // "symbol"
typeof undefined; // "undefined"
typeof (() => {}); // "function"
typeof class C {}; // "function"
typeof null; // "object"
typeof []; // "object"
typeof missingName returns "undefined" instead of throwing, but this exception applies only to an undeclared identifier used directly as the operand. It does not make arbitrary expressions safe, and accessing a lexical binding in its temporal dead zone still throws.
The legacy browser object document.all has a specification-defined compatibility exception and reports "undefined"; do not use it for feature detection.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents