Understanding JavaScript: Null vs Undefined
January 03, 2020
When you come across null
and undefined
you might be tempted to think that they are just synonym. In this tutorial you’ll discover that they are not. Let’s go.
Null
It represents an empty or non-existant value and it must be assigned.
const myVar = null // assign null to myVar
console.log(myVar) // shows null
In the example above, our variable (myVar
) has been set to null
and shows null
in the console.
The typeof
null
is object
(though this is an implementation error since the very beginning of JavaScript)
console.log(typeof myVar) // shows object
Undefined
It means that a variable exists (is declared) but has not yet assigned a value.
const myVar; // declaration of the var
console.log(myVar) // shows undefined
You can also explicitly assign undefined
to a variable like this:
const myVar = undefined
console.log(myVar) // shows undefined
If you try to look up a non-existing properties in an object, JavaScript will send you undefined
too:
const myObj = {
name: "Sarah",
age: 0,
}
console.log(myObj.gender) // shows undefined
Same if you don’t have an explicit return in a function
const myFun = () => {}
console.log(myFunr()) // shows undefined
Finally, the type of undefined
is undefined
.
const myVar; // declaration of the var
console.log(typeof myVar) // shows undefined
Conclusion
null
is a value that represents no value whereas undefined
is the default value for a variable that has not been assigned a specific value, or a function that has no explicit return value, or non-existing properties in an object.
Thank you for reading!