At some point while debugging, you've probably typed console.log(typeof something) just to figure out what on earth you're actually dealing with. That's basically the whole job of typeof, it looks at a value and tells you what type it is. Simple enough in theory, and it mostly is, except for a couple of famous quirks that trip up even developers who've been writing JavaScript for years.
Let's go through how typeof actually works, what it returns for each type, and the two weird exceptions worth knowing before they confuse you mid-debug.
Table of Contents
The Basics
typeof returns a string naming the type of whatever you give it.console.log(typeof 42); // "number" console.log(typeof "hello"); // "string" console.log(typeof true); // "boolean" console.log(typeof undefined); // "undefined" console.log(typeof Symbol()); // "symbol" console.log(typeof 10n); // "bigint"Nothing surprising so far, each primitive type gets its own clear, matching label.
Functions Get Their Own Label Too
This one's worth knowing, functions technically are objects in JavaScript, but typeof gives them a special label anyway, since knowing "this is callable" is genuinely useful information.
function greet() {
console.log("Hi!");
}
console.log(typeof greet); // "function"
If functions were just lumped in with regular objects, you'd lose an easy way to check "can I actually call this thing," so this small exception is a genuinely practical one.The Famous null Bug
Here's the one that catches almost everyone off guard the first time.console.log(typeof null); // "object"
That's just wrong, and everyone who's worked with JavaScript for a while knows it's wrong. It's a genuine bug baked into the very first version of the language back in 1995. By the time anyone noticed, way too much existing code already depended on that behavior, so fixing it would have broken things across the web. It's stayed that way ever since, one of JavaScript's most well-known historical quirks.
If you actually need to check for null specifically, skip typeof entirely and compare directly.
let value = null; console.log(value === null); // true
Arrays Say "Object" Too
This is another one worth remembering, since it surprises people expecting something like "array".let numbers = [1, 2, 3]; console.log(typeof numbers); // "object"Arrays are technically a specialized kind of object in JavaScript, so typeof just calls them what they are underneath. If you specifically need to check whether something is an array, use Array.isArray() instead.
console.log(Array.isArray(numbers)); // true
console.log(Array.isArray("hello")); // false
A Genuinely Handy Use Case
One of the more practical everyday uses of typeof is safely checking if a variable even exists before using it, without your code throwing an error if it doesn't.
if (typeof someVariable === "undefined") {
console.log("This variable hasn't been declared yet");
}
This works even if someVariable was never declared anywhere at all, which is genuinely useful. Comparing directly against an undeclared variable with something like someVariable === undefined would actually throw an error instead, so typeof is the safer choice here.Conclusion
typeof does exactly what it says on the label, for almost everything. The two exceptions worth actually remembering are that null incorrectly returns "object" thanks to a decades-old bug, and arrays also return "object" since they're technically a specialized kind of object under the hood. Once those two quirks are filed away in your memory, typeof becomes one of the most reliable, quick debugging tools you'll reach for constantly.Frequently Asked Questions (FAQs)
1.Why does typeof null return "object" instead of "null"?2.How do I actually check if something is an array then?It's a long-standing bug from JavaScript's original 1995 implementation, kept as-is because fixing it now would break too much code that already relies on that behavior.
3.Can typeof be used on a variable that was never declared, without causing an error?Use Array.isArray(value) instead of typeof, since typeof will just tell you "object" for arrays without distinguishing them from regular objects.
Yes, that's actually one of its most useful properties. typeof someUndeclaredVariable safely returns "undefined" instead of throwing an error, which makes it a safe way to check for a variable's existence.
0 Comments