One of the great mysteries of the world revolves around making sense of null and undefined. Most code you see is littered with them, and you've probably run into them yourself a few times. As mysteries go, making sense of null and undefined isn't particulary bizarre. It is just dreadfully boring...like the most boring (yet important) thing about JavaScript you'll ever have to learn.
Onwards!
Null
Let's start with null. The null keyword is a primitive that fills a special role in the world of JavaScript. It is an explicit definition that stands for no value. If you've ever browsed through code others have written, you'll probably see null appear quite a number of times. It is quite popular, for the advantage of null lies in its definitiveness. Instead of having variables contain stale values or mystery undefined values, setting it to null is a clear indication that you want the value to not exist.
This advantage is important when you are writing code and want to initialize or clear a variable to something that represents nothing.
Here is an example:
var name = null;
if (name === null) {
name = "Peter Griffin";
} else {
name = "No name";
}
The null primitive isn't a naturally occuring resource. It is something something you consciously assign, so you will often see it used as part of variable declarations or passed in as arguments to function calls. Using null is easy. Checking for its existience is pretty easy as well:
if (name === null) {
// do something interesting...or not
}
The only thing to note is that you should use the === operator instead of the lowly == one. While the world won't end if you use ==, it's good practice to check for both type and value when working with null.
Undefined
Here is where things get a little interesting. We see undefined in several different situations. A let declaration with no initializer sets its binding to undefined when the declaration executes. A function can return undefined, and a property lookup can find no value. Reading an undeclared name, or a lexical binding before its initialization, is a different situation and can throw instead.
Here is a code snippet that points out undefined in a few of its natural habitats:
let myVariable;
alert(myVariable); // undefined
function doNothing() {
// watch paint dry
return;
}
let weekendPlans = doNothing();
alert(weekendPlans); // undefined
let person = {
firstName: "Isaac",
lastName: "Newton"
}
alert(person.title); // undefined
In our code, we may need to check whether a value is undefined. If the binding has been initialized and the name undefined isn't shadowed locally, a direct comparison works like this:
if (myVariable === undefined) {
// do something
}
Modern JavaScript's global undefined property is non-writable; an assignment cannot replace it with true or another value. A local variable or parameter named undefined can still shadow that global name, though. Avoid that misleading naming choice. A typeof comparison doesn't depend on the value of a binding named undefined:
var myVariable;
if (typeof myVariable === "undefined") {
alert("Define me!!!");
}
This works for the declared variable shown here. The typeof operator also returns the string "undefined" for an unresolved identifier, but it isn't a universal readiness guard. If a let or const binding is still in its temporal dead zone, typeof throws just like other reads of that binding. Initialization order still matters.
null == undefined, but null !== undefined
Continuing the == and === weirdnesses, if you ever check for null == undefined, the answer will be a true. If you use === and have null === undefined, the answer in this case will be false.
Loose equality has a specific rule that matches null with undefined. It doesn't need to convert one into the other for this case. Strict equality keeps them distinct. Our application's contract can keep them distinct too: an update API might use null to clear a field and undefined to leave it unchanged. A loose-equality match doesn't make those instructions interchangeable.
A hat tip to senocular (aka Trevor McCauley) for pointing this out!
Conclusion
There is a reason why I saved these built-in types for last. Null and undefined are the least exciting of the bunch, but they are also often the ones that are the most misunderstood. Knowing how to use null and detecting for it and undefined is a very important skill to get right. Not getting it right will lead to very subtle errors that are going to be hard to pinpoint.
Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇

