How to Use the typeof Operator in JavaScript: Full Guide
The typeof operator tells you what type a value is in JavaScript. Learn how to use it, what it returns for each type, and the few gotchas to watch out for.
typeof is a built-in JavaScript operator that returns a string telling you what type a value is. You write it before any value or expression, and JavaScript gives you back a short type label.
console.log(typeof "hello"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"It is the fastest way to check what kind of data you are dealing with before you try to use type-specific methods on it.
The typeof Return Values
Every value in JavaScript produces one of these typeof results:
| Value | typeof Result |
|---|---|
| a string like "hello" | string |
| a number like 42, 3.14, NaN, or Infinity | number |
| true or false | boolean |
| undefined | undefined |
| null | object (legacy bug) |
| a plain object like {} | object |
| an array like [1, 2, 3] | object |
| a function | function |
| a Symbol | symbol |
| a BigInt like 9007199254740991n | bigint |
Notice three important surprises in this table: null returns object, arrays return object, and NaN returns number. The null case is especially confusing because null is a primitive, not an object. For a deeper comparison, see the undefined vs null guide.
Using typeof in Practice
Type guards before operations
The most common use of typeof is checking a value's type before you do something that only works with that type:
function getLength(value) {
if (typeof value === "string") {
return value.length;
}
return 0;
}
console.log(getLength("hello")); // 5
console.log(getLength(123)); // 0 (not a string, safe fallback)Without the typeof check, calling .length on a number would return undefined instead of crashing, but the intent is clearer with the guard.
Checking if a variable exists
typeof is the only operator that can safely check a variable that might not exist anywhere in your code:
// If someLibrary was never declared, this does NOT throw an error
if (typeof someLibrary !== "undefined") {
console.log("Library is loaded");
} else {
console.log("Library is missing");
}Any other way of referencing someLibrary, like writing a plain if check on it directly, would throw a ReferenceError if the variable does not exist. This pattern was common before ES modules, when scripts loaded in a specific order.
The typeof null Problem
typeof null returns "object", which is wrong. null is a primitive value that represents intentional absence of any value. It is not an object.
console.log(typeof null); // "object" -- this is a bug, not correct behaviorThis bug has existed since the very first version of JavaScript and cannot be fixed without breaking existing code on the web. The workaround is simple: never use typeof to check for null. Use strict equality instead:
const value = null;
// Wrong: typeof does not help
if (typeof value === "null") { /* never true */ }
// Right: direct comparison
if (value === null) {
console.log("Value is null");
}Arrays and typeof
Arrays are one of the most common values you will run typeof against, and they trip up beginners because JavaScript does not give arrays their own dedicated type. Arrays are technically objects in JavaScript, so typeof returns "object" for them:
console.log(typeof [1, 2, 3]); // "object"
console.log(typeof { name: "Alice" }); // "object"You cannot tell an array from a plain object using typeof alone, since both return the same result. Use Array.isArray() instead, which checks the internal array structure rather than the general object type:
const items = [1, 2, 3];
const user = { name: "Alice" };
console.log(Array.isArray(items)); // true
console.log(Array.isArray(user)); // falseArray.isArray() is reliable and works even when arrays come from another JavaScript context, like an iframe.
Functions and typeof
typeof on a function returns "function", even though functions are technically objects:
function greet() {
return "Hello";
}
console.log(typeof greet); // "function"This is a useful exception. There is no separate function type in JavaScript's core type system, but typeof gives you a practical way to check if something is callable:
function safeCall(maybeFunction) {
if (typeof maybeFunction === "function") {
return maybeFunction();
}
return "Not a function";
}
console.log(safeCall(() => "Hi")); // "Hi"
console.log(safeCall(42)); // "Not a function"typeof and NaN
NaN is one of the strangest values in JavaScript, and typeof adds one more surprise on top of it. NaN stands for Not a Number, but typeof NaN returns "number":
console.log(typeof NaN); // "number"This is technically correct because NaN is part of the IEEE 754 number specification that JavaScript follows. The value represents an invalid numeric operation result, but it still lives inside the number type. Use Number.isNaN() to check for NaN specifically:
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN("hello")); // falseFor more on NaN, see the NaN guide.
A Reliable Type-Checking Pattern
For everyday type checks, typeof is the right tool. Combine it with special-case checks for null and arrays when you need to handle all types:
function getType(value) {
if (value === null) return "null";
if (Array.isArray(value)) return "array";
return typeof value;
}
console.log(getType("hello")); // "string"
console.log(getType(42)); // "number"
console.log(getType(null)); // "null"
console.log(getType([1, 2])); // "array"
console.log(getType(() => {})); // "function"This pattern gives you accurate type labels for every value you will encounter in day-to-day JavaScript.
Rune AI
Key Insights
- typeof returns a string indicating the type of a value.
- typeof null returns 'object' -- use value === null instead.
- typeof on arrays returns 'object' -- use Array.isArray() for arrays.
- typeof on functions returns 'function'.
- typeof is safe to use on undeclared variables (returns 'undefined' without an error).
- Use typeof for type guards before performing type-specific operations.
Frequently Asked Questions
Why does typeof null return 'object'?
How do I check if a value is an array?
Does typeof work on undeclared variables?
Conclusion
typeof is JavaScript's built-in type checker. It returns a string like 'string', 'number', or 'object' that tells you what kind of value you are working with. Remember that typeof null is 'object' (a legacy bug), typeof on arrays returns 'object', and typeof on functions returns 'function'. Use typeof for quick type guards, but pair it with Array.isArray for arrays and strict equality for null checks.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.