JS typeof
In this page:
typeof value // "string", "number", "boolean", "object", "function", "undefined"
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.
Example: Basic typeof Checks
// Print `typeof "hello"` to the console
// Print `typeof "hello"` to the console
console.log(typeof "hello");
// Print `typeof 5` to the console
// Print `typeof 5` to the console
console.log(typeof 5);
// Print `typeof true` to the console
// Print `typeof true` to the console
console.log(typeof true);
// Print `typeof undefined` to the console
// Print `typeof undefined` to the console
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.
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.
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.
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.
Example: The instanceof Operator
// Declare the constant `arr`, set to `[1, 2, 3]`
// Declare the constant `arr`, set to `[1, 2, 3]`
const arr = [1, 2, 3];
// Declare the constant `date` as a new `Date` instance
// Declare the constant `date` as a new `Date` instance
const date = new Date();
// Print `arr instanceof Array` to the console
// Print `arr instanceof Array` to the console
console.log(arr instanceof Array);
// Print `date instanceof Date` to the console
// Print `date instanceof Date` to the console
console.log(date instanceof Date);
// Print `arr instanceof Date` to the console
// Print `arr instanceof Date` to the console
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