Note: It's my first blog post, constructive criticism would be welcome 🙂
I always had a small confusion between null and undefined in JavaScript.
...
For further actions, you may consider blocking this person and/or reporting abuse
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)
I thought the example explains that. When variables are declared but not initialized, Javascript assigns
undefined
to it. But when we assignnull
, we know that it is "empty" or "nothing" and later we can assign some value to it.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 tonull
instead fix or improve it?I don't think using
null
will fix or improve anything, it's just a good practice to follow.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.
Hope it helps,
stackoverflow.com/a/6604783/9243226
yeah.. it just seems to say that having both is a bit redundant.
Good and neat explanation which will help us to understand the concepts in more interesting way.
Thank you 🙂