Type Detection: typeof, instanceof & Object.prototype.toString
Type Detection & typeof Operator
How do you find out what kind of data a variable holds? In JavaScript, the built-in typeof operator is your first tool—but it has famous historical quirks. Learning which tool to use for each data type ensures your code runs safely without crashing.
The Core Idea: The 3 Detection Tools
Because JavaScript evolved over decades, no single operator works perfectly for everything. We use 3 main tools:
typeof Operator
The fastest way to test primitives (numbers, strings, booleans, undefined) and functions. Always returns a lowercase string.
Array.isArray()
Since typeof [] returns "object", JavaScript added this method to accurately distinguish arrays from plain objects.
Object.prototype.toString
The ultimate engine-level detector. Queries the internal [[Class]] tag of any value without failing on null, Dates, or RegExps.
┌────────────────────────────────────────────────────────┐
│ WHAT VALUE ARE YOU TRYING TO TEST? │
└───────────────────────────┬────────────────────────────┘
│
┌──────────────────┼──────────────────┐
▼ ▼ ▼
┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐
│ IS IT PRIMITIVE │ │ IS IT AN ARRAY? │ │ IS IT AN OBJECT │
│ OR A FUNCTION? │ │ │ │ OR NULL? │
├──────────────────┤ ├──────────────────┤ ├──────────────────┤
│ Use: typeof x │ │ Use: │ │ Step 1: │
│ │ │ Array.isArray(x) │ │ Check x === null │
│ Returns: │ │ │ │ Step 2: │
│ "number" │ │ Returns: │ │ typeof x === │
│ "string" │ │ true or false │ │ "object" │
│ "boolean" │ │ │ │ │
│ "undefined" │ │ (Never rely on │ │ (Guards against │
│ "function" │ │ typeof for []) │ │ typeof null!) │
└──────────────────┘ └──────────────────┘ └──────────────────┘How They Work: Normal Cases vs Famous Quirks
Compare how JavaScript behaves with standard primitives versus its surprising historical quirks:
typeof 42; // "number"
typeof "hello"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof 10n; // "bigint"
typeof (() => {}); // "function"Takeaway: For native primitives and callable functions, typeof is completely reliable and fast.
typeof null; // "object" ⚠️ (1995 Bug)
typeof [1, 2]; // "object" ⚠️ (Arrays are objects)
typeof NaN; // "number" ⚠️ (Numeric IEEE state)
// The proper detections:
val === null; // Check null directly
Array.isArray([1, 2]); // trueTakeaway: Never check for null or [] with typeof alone. Use direct equality and Array.isArray().
The Classic Mistake: The `null` Object Crash & The Fix
Because typeof null === "object", checking if a variable is an object without guarding against null causes runtime crashes:
function printUser(user) {
if (typeof user === "object") {
// If user is null, this throws TypeError!
console.log(user.name);
}
}
printUser(null); // 💥 Uncaught TypeError!When user is null, the condition passes, but accessing null.name crashes your app.
function printUser(user) {
// Check user !== null FIRST!
if (user !== null && typeof user === "object") {
console.log(user.name); // 100% Safe
}
}
printUser(null); // Clean & Safe: does not crashAlways add user !== null before checking typeof user === "object".
Complete Type Detection Cheat Sheet
Keep this quick reference handy to know what typeof returns for every JavaScript value:
| Expression | typeof Result | Recommended Way to Check | Explanation |
|---|---|---|---|
| typeof 42 | "number" | typeof x === "number" | Standard primitive number |
| typeof "hello" | "string" | typeof x === "string" | Text string primitive |
| typeof true | "boolean" | typeof x === "boolean" | Boolean truth value |
| typeof undefined | "undefined" | x === undefined | Variable without an assigned value |
| typeof null | "object" ⚠️ | x === null | Famous 1995 engine bug; null is primitive |
| typeof [1, 2] | "object" ⚠️ | Array.isArray(x) | Arrays inherit from Object; use Array.isArray |
| typeof {} | "object" | x !== null && typeof x === "object" | Plain JavaScript object |
| typeof (() => {}) | "function" | typeof x === "function" | Functions get special first-class typeof |
| typeof NaN | "number" ⚠️ | Number.isNaN(x) | IEEE-754 numerical state; test with Number.isNaN |
Quick Knowledge Check
Test your understanding on these 3 simple questions (one by one):
What is the core purpose of Type Detection: typeof, instanceof & Object.prototype.toString in JavaScript?
// Concept 9: Type Detection: typeof, instanceof & Object.prototype.toString
// Understanding the core mental modelSelect an answer: