null vs undefined In Javascript
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.
Subscribe to my newsletter
Read articles from Sandeep Rana directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sandeep Rana
Sandeep Rana
I'm a dedicated ServiceNow Developer and Analyst with four years of experience. I previously worked at Deloitte and am currently with QBRAINX. My journey in technology started as a freelance web developer, where I developed a passion for creating user-friendly web solutions. In my current role, I specialize in various aspects of ServiceNow, including Portal design, Flow, Integration, Common Configuration, and HRSD modules. What truly excites me is experimenting with the amalgamation of web development and ServiceNow capabilities. My work allows me to blend creativity with technical prowess, ensuring the solutions I create are both functional and intuitive. I bridge the gap between complex technical concepts and user-friendly designs, striving for excellence in every project. Beyond my professional endeavors, I'm a lifelong learner, constantly exploring new technological horizons. My enthusiasm for innovation fuels my commitment to delivering high-quality results. If you share a passion for technology and innovation, I'd love to collaborate and create something extraordinary together. Let's connect!