DEV Community

Discussion on: Null vs Undefined in JavaScript in 1 min

Collapse
 
savagepixie profile image
SavagePixie • Edited

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

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

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