Skip to main content
Practice Problems

The typeof operator in JavaScript

What is typeof?

The typeof operator returns a string indicating the type of the operand. It's the simplest way to check a value's type at runtime.


typeof Results

javascript
typeof 42; // "number" typeof "hello"; // "string" typeof true; // "boolean" typeof undefined; // "undefined" typeof Symbol("id"); // "symbol" typeof 42n; // "bigint" typeof function(){}; // "function" typeof {}; // "object" typeof []; // "object" ⚠️ typeof null; // "object" ⚠️ (historical bug) typeof NaN; // "number" ⚠️

Complete Reference Table

Valuetypeof result
42, 3.14, NaN, Infinity"number"
"hello", 'world', \template``"string"
true, false"boolean"
undefined"undefined"
null"object" ⚠️
Symbol()"symbol"
42n"bigint"
{}, [], new Date()"object"
function(){}"function"

Common Gotchas

typeof null === "object"

javascript
typeof null; // "object" — this is a famous bug from JavaScript's creation // ✅ Proper null check value === null; value == null; // checks both null and undefined

Arrays are "object"

javascript
typeof []; // "object" // ✅ Check for arrays Array.isArray([]); // true [] instanceof Array; // true Object.prototype.toString.call([]); // "[object Array]"

NaN is "number"

javascript
typeof NaN; // "number" // ✅ Check for NaN Number.isNaN(NaN); // true Number.isNaN("hello"); // false (doesn't coerce) isNaN("hello"); // true ⚠️ (coerces to number first)

Better Type Checking

Object.prototype.toString.call()

javascript
Object.prototype.toString.call(42); // "[object Number]" Object.prototype.toString.call("str"); // "[object String]" Object.prototype.toString.call(true); // "[object Boolean]" Object.prototype.toString.call(null); // "[object Null]" Object.prototype.toString.call(undefined); // "[object Undefined]" Object.prototype.toString.call([]); // "[object Array]" Object.prototype.toString.call({}); // "[object Object]" Object.prototype.toString.call(new Date()); // "[object Date]" Object.prototype.toString.call(/regex/); // "[object RegExp]" Object.prototype.toString.call(new Map()); // "[object Map]"

Practical Type Check Utility

javascript
function getType(value) { if (value === null) return "null"; if (Array.isArray(value)) return "array"; return typeof value; } getType(null); // "null" getType([]); // "array" getType({}); // "object" getType(42); // "number" getType(undefined); // "undefined"

typeof with Undeclared Variables

javascript
// typeof is safe with undeclared variables typeof undeclaredVar; // "undefined" (no error!) // Without typeof — throws console.log(undeclaredVar); // ReferenceError!

Important:

Remember the quirks: typeof null === "object" and typeof [] === "object". Use Array.isArray() for arrays, === null for null checks, and Number.isNaN() for NaN checks. For precise type detection, use Object.prototype.toString.call().

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

Finished reading?
Practice Problems