JS typeof
In this page:
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
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
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
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
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
const arr = [1, 2, 3];
const date = new Date();
console.log(arr instanceof Array);
console.log(date instanceof Date);
console.log(arr instanceof Date);
- Forgetting that typeof null returns object, a long-standing quirk rather than the null you might expect.
- Using typeof to distinguish arrays from plain objects, both report as object, Array.isArray() is needed for that distinction.
- 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.
- 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.
typeof and instanceof are core JavaScript operators supported identically across every browser and JavaScript engine.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic