JavaScript is a dynamically typed language, meaning you don’t need to declare variable types explicitly. While this provides flexibility, it can also lead to confusion. That’s where the typeof operator becomes useful. It helps you determine the data type of a given variable or value at runtime.
What is typeof?
The typeof operator in JavaScript is used to return a string indicating the type of the unevaluated operand. It can be applied to any variable, literal, or expression.
Syntax:
javascriptCopyEdittypeof operand
or
javascriptCopyEdittypeof(operand)
Both syntaxes are valid, but the first one is more common.
Basic Examples
Here are some simple examples:
javascriptCopyEdittypeof 42; // "number"
typeof 'hello'; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object" (this is a well-known JavaScript quirk)
typeof Symbol('id'); // "symbol"
typeof BigInt(10); // "bigint"
typeof {}; // "object"
typeof []; // "object"
typeof function(){}; // "function"
Data Types Detected by typeof
The typeof operator returns the following string values for these types:
| JavaScript Type | typeof Result |
|---|---|
| Number | “number” |
| String | “string” |
| Boolean | “boolean” |
| Undefined | “undefined” |
| Symbol | “symbol” |
| BigInt | “bigint” |
| Object (including arrays and null) | “object” |
| Function | “function” |
Special Cases to Know
nullreturns"object"
This is a long-standing bug in JavaScript. Despitenullbeing a primitive type,typeof nullreturns"object". So be careful when checking fornull. javascriptCopyEdittypeof null; // "object"To reliably check fornull, use: javascriptCopyEditvalue === null- Arrays are Objects
Arrays return"object"as well, because in JavaScript, arrays are technically a type of object. javascriptCopyEdittypeof [1, 2, 3]; // "object"To differentiate between objects and arrays, use: javascriptCopyEditArray.isArray([1, 2, 3]); // true - Functions are Objects Too
Functions are a subtype of object buttypeofreturns"function": javascriptCopyEdittypeof function() {}; // "function"
Practical Use Cases
- Type Checking
Before performing operations on a variable, you might want to ensure it’s of a certain type: javascriptCopyEditif (typeof value === "number") { console.log(value * 2); } - Debugging
When debugging code,typeofis handy for quickly checking what type a variable holds: javascriptCopyEditconsole.log(typeof myVar); // e.g., "undefined" - Validating Parameters
If you’re writing functions that expect specific types,typeofhelps with validation: javascriptCopyEditfunction greet(name) { if (typeof name !== "string") { throw new Error("Expected a string"); } console.log("Hello, " + name); }
Limitations of typeof
- It cannot distinguish between arrays and plain objects.
- It treats
nullas an object. - It doesn’t provide detailed type information for custom objects or classes.
For more advanced type checks, you might combine typeof, instanceof, and Object.prototype.toString.call().
Conclusion
The typeof operator is a simple yet powerful tool in JavaScript. It helps you understand and debug your variables at runtime, making it essential for any developer. However, be aware of its quirks, especially around null and arrays, and use complementary methods when deeper type checking is needed.