Skip to content
Concept 09 // Module 01: JavaScript Foundations & Environment

Type Detection: typeof, instanceof & Object.prototype.toString

Module 01: Types & Values•Concept 02 of 100• 5 min read

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:

Tool 1 • Built-in OperatorFastest

typeof Operator

The fastest way to test primitives (numbers, strings, booleans, undefined) and functions. Always returns a lowercase string.

typeof 42 → "number"
Tool 2 • Array SpecialistReliable

Array.isArray()

Since typeof [] returns "object", JavaScript added this method to accurately distinguish arrays from plain objects.

Array.isArray([]) → true
Tool 3 • Universal Tag100% Accurate

Object.prototype.toString

The ultimate engine-level detector. Queries the internal [[Class]] tag of any value without failing on null, Dates, or RegExps.

toString.call(null) → "[object Null]"
Type Detection Decision Tree:
┌────────────────────────────────────────────────────────┐
│             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:

Example 1 • Primitives Work Perfectly
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.

Example 2 • The 3 Historical Quirks
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]); // true

Takeaway: 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:

The Mistake (Crashes on null)
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.

The Fix (Guard against null & arrays)
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 crash

Always 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:

Expressiontypeof ResultRecommended Way to CheckExplanation
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 === undefinedVariable without an assigned value
typeof null"object" ⚠️x === nullFamous 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):

Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model

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 model

Select an answer:

Live Type Detection Studio

typeof inspects primitive values reliably, returning string type names like "number", "string", and "boolean".
JavaScript Editor
Loading...
Console Output
Waiting for output...