null: Intentional Absence
You'll learn how to use null to signal "no value on purpose" and discover a quirky JavaScript behavior.
What is null?
While undefined means "JavaScript hasn't assigned anything yet," null is a value you set deliberately to say "this should be empty right now." Think of it like the difference between an empty parking spot that's simply vacant (undefined) versus one with a "Reserved" sign on it (null)—both are empty, but null shows intention.
When to Use null
You use null when you want to explicitly represent "no object" or "no value." For example, if you're tracking a user's profile picture and they haven't uploaded one yet, you might set it to null to show you've checked and there's intentionally nothing there.
console.log(null); // null
The typeof null Quirk
Here's something strange that has confused JavaScript developers for decades:
console.log(typeof null); // "object"
Wait, what? null shows up as "object"! This is actually a bug from JavaScript's earliest days that's now too late to fix without breaking millions of websites. Just remember: null is not an object—it's its own primitive type representing intentional absence.
null vs undefined
Both represent "no value," but:
undefined= JavaScript's default when nothing is setnull= You intentionally saying "nothing here"
let a;
console.log(a); // undefined (JavaScript's doing)
let b = null;
console.log(b); // null (your choice)
Key Takeaway: Use null to explicitly represent "intentionally no value," and remember that typeof null incorrectly returns "object" due to a historical JavaScript bug.