Table of contents
Basic Definition
null and undefined is part of Javascript primitive datatypes living happily with 5 others.
let javascriptPrimitives = ['string','number','null','undefined','boolean','symbol', 'bigint'];
Note - Both null and undefined will represent the false value but still both have a different meaning
null
null represents an absence of value. It means that you have particularly and intentionally defined that the variable won't hold any value so it is null.
It's like saying, "I know there should be a value here, but I'm explicitly saying it's empty"
const myLoveLife = null
undefined
undefined also represents an absence of value but in a different way.
It is used when a variable is declared but not assigned a value or when a function parameter is not provided when the function is called.
It's like saying, "I don't know if there should be a value here, but I haven't assigned one yet."
const bugFreeCode = undefined
A Simpler Way
Let's break it down to a scenario
Let's say you have sent an email to yourself from another account and when you open it there is nothing.
It is like saying, I am sending a mail but it will intentionally be blank or nullNow, let's say you have received an email from some XYZ person and it is blank. So you would think that "Was there supposed to be something here" because you cannot see anything in the mail. The mail is not defined or undefined
A Funny Way
Let's understand the same thing through a joke
null and undefined went to a party and wanted to represent the absence of food in their own way.
null was the life of the party, proudly declaring, "My plate is intentionally empty! Cheers to being deliberately null!"
On the other hand undefined was a bit clueless, wandering around asking, "Am I supposed to have food? I don't know! I'm undefined!"
Ending Note
null and undefined both have their own unique use cases and both represent the false value. Do use them with caution.