← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 10 of 26

JS typeof

Imagine a customs officer at a border, quickly inspecting a package and stamping it with a category label, food, electronics, documents, before it moves further along. The typeof operator is that inspector for JavaScript values, it looks at whatever you give it and returns a string describing its type, letting your code make decisions based on what kind of data it's actually dealing with. This kind of runtime type checking is common in real code, like validating that a form field on cookiescursor.com actually received a number before running a calculation on it.

Basic typeof Checks

typeof followed by a value or variable returns a string naming its type, string, number, boolean, undefined, object, function, symbol, or bigint.

Note: typeof can be used both with and without parentheses, typeof value and typeof(value) behave identically.

Warning: typeof always returns a string, so comparing its result requires quotes, like typeof x === string, not typeof x === string.

Example: Basic typeof Checks

javascript
console.log(typeof "hello");
console.log(typeof 5);
console.log(typeof true);
console.log(typeof undefined);

The null Gotcha

typeof null returns object, which is widely considered a bug baked into the earliest versions of JavaScript, kept ever since purely for backward compatibility with existing code.

Note: To reliably check for null specifically, compare directly with strict equality, value === null, rather than relying on typeof.

Warning: Always remember typeof null is object, not null, it's one of the most frequently cited quirks in the entire language.

Example: The null Gotcha

javascript
console.log(typeof null); // "object" - a long-standing bug

typeof With Functions

typeof reports functions with their own distinct type, function, separate from the general object category, even though functions are technically a specialized kind of object in JavaScript.

Note: typeof value === function is a common, reliable way to confirm something is callable before actually calling it.

Warning: Arrow functions, regular functions, and function expressions all report function identically, typeof cannot distinguish between these different function styles.

Example: typeof With Functions

javascript
function greet() {}
console.log(typeof greet); // "function"

typeof With Arrays and Objects

Both arrays and plain objects report as object under typeof, since arrays are technically a specialized kind of object in JavaScript, so typeof alone cannot tell them apart.

Note: Use Array.isArray(value) specifically whenever you need to confirm something is truly an array, typeof is not sufficient for that check.

Warning: Relying on typeof value === object to mean 'plain object' will incorrectly also match arrays and null, additional checks are needed for precision.

Example: typeof With Arrays and Objects

javascript
console.log(typeof [1, 2, 3]); // "object"
console.log(typeof { a: 1 }); // "object"

The instanceof Operator

instanceof checks whether an object was created from a specific constructor, offering more precise type checking than typeof for distinguishing between different kinds of objects, like arrays versus dates versus custom classes.

Note: Use instanceof when typeof's broad object category isn't specific enough, like confirming a value is specifically a Date object.

Warning: instanceof can behave unexpectedly across different execution contexts, like separate browser iframes, where each has its own distinct constructor references.

Example: The instanceof Operator

javascript
const arr = [1, 2, 3];
const date = new Date();
console.log(arr instanceof Array);
console.log(date instanceof Date);
console.log(arr instanceof Date);
Common Mistakes
  1. Forgetting that typeof null returns object, a long-standing quirk rather than the null you might expect.
  2. Using typeof to distinguish arrays from plain objects, both report as object, Array.isArray() is needed for that distinction.
  3. Assuming typeof works the same way for checking if a variable exists at all versus checking its current value's type, they're related but distinct concerns.
Chapter Summary
  • typeof returns a string identifying a value's type, such as string, number, or boolean.
  • typeof null famously returns object due to a long-standing historical bug preserved for compatibility.
  • typeof a function returns function, and instanceof is used for more specific object type checks, like confirming an array.
Browser Support

typeof and instanceof are core JavaScript operators supported identically across every browser and JavaScript engine.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.