I've been writing JavaScript for quite a long time now, and I have never had a reason to use null
. It seems that undefined
is always preferable and serves the same purpose programmatically. What are some practical reasons to use null
instead of undefined
?
I don't really have an answer, but according to Nicholas C. Zakas, page 30 of his book "Professional JavaScript for Web Developers":
When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to
null
as opposed to anything else. That way, you can explicitly check for the valuenull
to determine if the variable has been filled with an object reference at a later time