A complaint I hear sometimes from folks programmers learning JavaScript is that it has both null and undefined. If JavaScript is your first programming language, this may not seem weird, but once you compare it to others… yeah, it’s strange.
I have some suspicions for why this is and they may not be right but they do help me sleep at night and make the night terrors stop pestering me with their = signs. So let’s get into it.

Null
Many programming languages have null. In general, null will mean, “no value”. Quite humorously, it’s a value whose only job it is to tell you, “there’s no value.” Some, like its creator, consider this a mistake.
So keep in mind that almost everything in JavaScript is an object: primitive constructors, arrays, even functions. All are objects.
so I think Brendan Eich, when he was creating JavaScript, looked at these other languages with null and thought, “yeah, we need that, too.” And because everything is an object, JavaScript’s null actually has a more specific meaning of “no object”.
So, if generally speaking, null’s job is to tell you, “no value,” and in JavaScript null’s job is to tell you, “no object,” then it kind of makes sense why null is an object:
typeof null === ‘object’; // true
…kinda…
I think null is there for the programmers so that we can return it in functions where we want to say, “there should have been an object, but I couldn’t create one”. E.g.:
function getAddress(person) {
if (!person) return null;
const getData(person);
return person.address;
}
undefined
In JavaScript undefined pulls double-duty by telling us two things:
- No value assigned
- Not declared
No Value Assigned
function showThing() {
let thing; // declared the variable, but no value is assigned
console.log(thing); // undefined
}
- The code declares that
thingexists. - But
thinghas no value assigned to it. - If it exists, but has no value, then the value will be
undefined.
As you can imagine, we don’t explicitly assign variables to undefined because it’s redundant. undefined is already there lurking in the background:
function showThing() {
let thing = undefined; // don't do this. it's unnecessary
console.log(thing); // undefined
}
Not Declared — on an object
const list = ['food', 'water']; // declared variable. assigned console.log(list[2]); // undefined
- The code declares that
listexists. - Then it goes a step further and assigns values to the
0and1properties on it. But there is no property called 3 declared on it. - So if you ask for an undeclared property on an object, you will get the value
undefined
Not Declared — anywhere
typeof someNonExistentVariable; // undefined
- Absolutely zero code ever speaks
someNonExistentVariableinto existence; it is completely undeclared - But you wanted to know what type it had
- The type of a thing that definitely does not and has never existed will be
undefined
tl;dr What’s the rationale behind JavaScript supporting both null and undefined
nullis for us to use, in our programs, to tell someone, “The object you wanted isn’t there; here’s an object you don’t want”undefinedis the default state of the JavaScript universe- functions that don’t explicitly return a value will return
undefined - properties that don’t exist on objects are
undefined - the type of variables that don’t exist is
undefined
- functions that don’t explicitly return a value will return
undefinedis for our compiler/interpreter, to tell us, “there’s no value there.”
JavaScript was designed to be a very fault-tolerant language. It’s why it has dynamic typing, and uses type coercion — they aren’t mistakes of the language. They were intentional decisions to not punish the end-user for the mistakes of the programmer.
So undefined is there to give you some answer — whenever you ask for a value.
Which… BTW, is why this makes a lot of sense:
function showThing() {
console.log(typeof someNonExistentVariable); // undefined
console.log(someNonExistentVariable); //ReferenceError: someNonExistentVariable is not defined
}
showThing()
If you ask for the type of something that isn’t real, JavaScript is polite enough to give you an answer. If you use something that isn’t real, JavaScript is going to tell you why it couldn’t.
It’s maybe not a perfect situation, but maybe now we at least understand a little bit more why JavaScript feels the need to be so difficult.