Why You Should Use `null` Instead Of `undefined` To Represent The Absence Of A Value In Javascript
Many entities in a JS runtime environment can turn out to be undefined
. According to ES specs, “Any variable that has not been assigned a value has the value undefined
.” It strikes me as conceptually inconsistent to tack onto the above that any variable that has been assigned a value can also have the value undefined
to signify the absence of any value.
By contrast, according to MDN, “The value null
represents the intentional [i.e., developer-generated] absence of any object value.”
Conclusion: best practice/convention: use null
.
Written on August 8, 2018