I was looking through idiomatic.js and notice the following lines in the type checking secton:
undefined:
Global Variables:
typeof variable === "undefined"
Local Variables:
variable === undefined
This section appears to originally be based on the jQuery style guide
I'd been taught that the first form was safer since undefined
can be redefined to another value. I also don't know of any reason that the 1st form doesn't work for local variables.
It seems to work fine in this fiddle. Can anyone explain why this style makes sense?
var undefined = 2;
function test(){
var x;
alert("1: " + (typeof x == "undefined"));
alert("2: " + ( x === undefined));
}
test(); //alerts "1: true", then "2: false"