What reason is there to use null instead of undefined in JavaScript?

Jimmy Cuadra picture Jimmy Cuadra · Jul 7, 2011 · Viewed 36.8k times · Source

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?

Answer

bbg picture bbg · Jul 7, 2011

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 value null to determine if the variable has been filled with an object reference at a later time