What is the usage of typeof in javascript?
Categories:
Understanding JavaScript's typeof
Operator: A Comprehensive Guide

Explore the typeof
operator in JavaScript, its behavior with different data types, common pitfalls, and best practices for accurate type checking.
The typeof
operator is a fundamental part of JavaScript, used to determine the data type of a variable or an expression. While seemingly straightforward, its behavior can sometimes be surprising, especially with certain primitive types and objects. This article will delve into how typeof
works, its return values, common quirks, and when to use it effectively for robust type checking in your JavaScript applications.
Basic Usage and Return Values
The typeof
operator returns a string indicating the type of its operand. It's a unary operator, meaning it operates on a single operand. Here's a breakdown of its return values for various JavaScript data types:
console.log(typeof 42); // "number"
console.log(typeof 'hello'); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof Symbol('id')); // "symbol" (ES6+)
console.log(typeof 10n); // "bigint" (ES11+)
console.log(typeof null); // "object" (a long-standing bug)
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"
console.log(typeof new Date()); // "object"
console.log(typeof /regex/); // "object"
Examples of typeof
with different JavaScript data types
typeof
is that typeof null
returns 'object'
. This is a historical bug in JavaScript that cannot be fixed without breaking existing code. Always be mindful of this when checking for null
.Understanding the typeof null
Anomaly
The typeof null === 'object'
result is a common source of confusion for JavaScript developers. It stems from the very first version of JavaScript. In the initial implementation, JavaScript values were represented as a type tag and a value. For objects, the type tag was 0
. null
was represented as the NULL
pointer, which had 0
as its type tag. Consequently, typeof null
incorrectly evaluates to 'object'
. While this is a bug, it's deeply ingrained in the language and is unlikely to change.
flowchart TD A[Value] --> B{Is it null?} B -- Yes --> C["typeof null" returns "object"] B -- No --> D{Is it an object?} D -- Yes --> E["typeof object" returns "object"] D -- No --> F{Is it a function?} F -- Yes --> G["typeof function" returns "function"] F -- No --> H[Other primitives: number, string, boolean, undefined, symbol, bigint]
Decision flow for typeof
operator, highlighting the null
anomaly
Advanced Type Checking and Best Practices
Due to the limitations of typeof
(especially with null
and distinguishing between different object types like arrays, dates, or custom objects), more robust methods are often needed for precise type checking. Here are some common approaches:
null
or undefined
, a simple strict equality check (=== null
or === undefined
) is usually sufficient and more accurate than typeof
.// Checking for null explicitly
const myVar = null;
if (myVar === null) {
console.log('myVar is null');
}
// Checking for arrays
const myArray = [1, 2, 3];
console.log(Array.isArray(myArray)); // true
// Checking for specific object types using instanceof
class MyClass {}
const myInstance = new MyClass();
console.log(myInstance instanceof MyClass); // true
console.log(myInstance instanceof Object); // true
// Using Object.prototype.toString for precise type checking
function getType(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
}
console.log(getType([])); // "Array"
console.log(getType({})); // "Object"
console.log(getType(new Date())); // "Date"
console.log(getType(null)); // "Null"
console.log(getType(undefined)); // "Undefined"
console.log(getType(42)); // "Number"
More precise type checking methods in JavaScript
While typeof
is excellent for quickly identifying primitive types and functions, it falls short when you need to differentiate between various object types or specifically check for null
. Combining typeof
with other techniques like Array.isArray()
, instanceof
, or Object.prototype.toString.call()
provides a more comprehensive and reliable approach to type checking in JavaScript.