If undefined is JavaScript's way of saying "nobody's gotten around to setting this yet," null is more like someone walking up and deliberately saying "there's nothing here, on purpose." It's a small distinction, but it actually matters a lot once you're writing code other people have to read and trust.
Let's go through what null actually represents, why it exists alongside undefined instead of just replacing it, and the one genuinely strange quirk about it that catches almost everyone off guard at some point.
Table of Contents
Null Means "Intentionally Empty"
let selectedUser = null; console.log(selectedUser); // nullThis is genuinely useful in real code. Imagine a variable that's supposed to hold a user object once someone logs in.
let currentUser = null; // no one has logged in yet
function login(user) {
currentUser = user;
}
login({ name: "Vi" });
console.log(currentUser); // { name: "Vi" }
Setting it to null up front makes your intent obvious to anyone reading the code, this variable will hold a user eventually, but right now, it deliberately holds nothing.The Famous typeof Quirk
Here's the one that surprises almost everyone the first time they see it.console.log(typeof null); // "object"
That looks completely wrong, and honestly, it is. This is a genuine bug that's existed since the very first version of JavaScript, back in 1995. By the time anyone realized it was a mistake, so much existing code already depended on that behavior that fixing it would have broken huge parts of the web. So it just stayed, and now it's one of those quirks every JavaScript developer eventually learns about the hard way.
If you actually need to check whether something is null, don't rely on typeof, compare it directly instead.
let value = null; console.log(value === null); // true
Null vs Undefined, One More Time
These two get compared constantly, and for good reason, they represent two very different kinds of "nothing."let a; // undefined, JavaScript did this automatically let b = null; // null, a developer set this on purpose console.log(a); // undefined console.log(b); // null
A simple way to remember the difference: undefined usually means "this was never set." null means "this was set, to nothing, deliberately." One is an accident of omission, the other is an intentional decision.
They do line up when you use loose equality though, which trips people up if they're not expecting it.
console.log(null == undefined); // true console.log(null === undefined); // falseLoose equality treats them as equivalent since they both represent an absence of value. Strict equality checks the type too, and since they're technically different types, it correctly says they're not equal.
Null Is Falsy
null is one of the small handful of falsy values in JavaScript, so it behaves the way you'd expect inside a condition.
let data = null;
if (!data) {
console.log("No data available yet");
}
This makes it genuinely convenient for checking whether something has been set before trying to use it, especially in situations like an API response that hasn't come back yet.Conclusion
null isn't complicated once you separate it clearly from undefined in your head, it's simply the value you choose when you want to say "nothing here, and I mean that on purpose." The typeof null quirk is just a historical accident worth knowing about rather than something to actually worry over. Once that distinction is clear, using null deliberately in your own code becomes a genuinely useful habit, not just another confusing value floating around your codebase.Frequently Asked Questions (FAQs)
1.When should I use null instead of just leaving a variable undefined?2.Why does typeof null return "object" instead of "null"?Use null whenever you want to clearly signal that the absence of a value is intentional, like a user who hasn't logged in yet, rather than a variable you simply forgot to set.
3.Does null == 0 return true, since both seem like "nothing"?It's a long-standing bug from JavaScript's original 1995 implementation. It's been kept as-is because fixing it now would break too much code that already relies on that behavior across the web.
No, that returns false. Loose equality only treats null as equal to undefined, not to 0, an empty string, or any other falsy value.
0 Comments