undefined is not a reserved word
With the new release of Prototype (1.5.0) comes a new website for the Prototype JavaScript Library. The home-page of the new website features a snippet of JavaScript:
cells: function (row) {
if (row == undefined) return this.something;
return $(row).getElementsBySelector(/* something */);
},
Let’s get this straight: undefined is not a reserved word. It’s safer and more logical to make the comparison to null:
cells: function (row) {
if (row == null) return this.something;
return $(row).getElementsBySelector(/* something */);
},
As null is a reserved word (and it’s quicker to type) I’d expect the Prototype community to favour it.
Note: assuming undefined hasn’t been redefined, the two code fragments are functionally identical. i.e. the equals operator (==) treats null and the primitive value undefined exactly the same.
Update: JavaScript has changed since this post was written: you cannot overwrite the undefined value in strict mode, nor in JavaScript modules (as modules run in strict mode by default.)