loading...

Null vs Undefined in JavaScript in 1 min

bgopikrishna profile image Gopi Krishna Originally published at gopikrishna.dev Updated on ・1 min read

Note: It's my first blog post, constructive criticism would be welcome 🙂

I always had a small confusion between null and undefined in JavaScript.

Now, Let's dive into the topic.

When we declare a variable without assigning any value to it, its value will be undefined by default.

let color;
console.log(color); //undefined

But when we assign null to a variable, we are explicitly assigning a "nothing" or "empty" value to it.

For example, we have a userDetails variable which stores the details of an user.
At first, it doesn't have any data, so we are assigning null to it.

let userDetails = null;

Later we fill the userDetails variable with the response from our function getUserDetails. The function may be a call to an API or accessing localStorage for details etc. Here it’s just a simple function which returns an object.

function getUserDetails() {
  return {
    userName: 'gk',
    id: '1',
  };
}

userDetails = getUserDetails();
console.log(userDetails); // {userName:"gk", id:"1"}

If the value is unknown at the time of variable definition, it's always best to use null.

This article was originally published on my blog.

Thank you.

Reference

undefined - MDN
null - MDN
Javascript Grammer

Discussion

pic
Editor guide
Collapse
savagepixie profile image
SavagePixie

You make the statement "If the value is unknown at the time of variable definition, it's always best to use null," but you don't defend it. I think your point would be greatly improved if you explained why null is better than undefined (and so help other people better understand the concept.)

(Edit: unless I have somehow missed it in the article, of course)

Collapse
bgopikrishna profile image
Gopi Krishna Author

I thought the example explains that. When variables are declared but not initialized, Javascript assigns undefined to it. But when we assign null, we know that it is "empty" or "nothing" and later we can assign some value to it.

Collapse
savagepixie profile image
SavagePixie

Yes, that's right. I guess that what I'm trying to understand is more along the lines of why is it bad for your code to have a variable set as undefined? (ie, what sorts of problems does it cause?) and how does setting it to null instead fix or improve it?

Thread Thread
bgopikrishna profile image
Gopi Krishna Author

I don't think using null will fix or improve anything, it's just a good practice to follow.

Collapse
aminmansuri profile image
hidden_dude

I wonder why we even need an undefined in Javascript. Wouldn't it just be better to default to null?
I fail to see a scenario in which having undefined (vs null) is useful.

Collapse
bgopikrishna profile image
Collapse
aminmansuri profile image
hidden_dude

yeah.. it just seems to say that having both is a bit redundant.

Collapse
sahajranipa1 profile image
sahaj ranipa

Good and neat explanation which will help us to understand the concepts in more interesting way.

Collapse
bgopikrishna profile image
Gopi Krishna Author

Thank you 🙂