JSLint is not passing this as a valid code:
/* global someVar: false */
if (typeof someVar === \"undefined\") {
var someVar = \"hi!\";
}
<
I think this is actually a problem with JSLint. It will issue the following error:
Unexpected 'typeof'. Compare directly with 'undefined'.
I believe this is bad advice. In JavaScript, undefined
is a global variable that is, usually, undefined. But some browsers allow scripts to modify it, like this: window.undefined = 'defined'
. If this is the case, comparing directly with undefined
can lead to unexpected results. Fortunately, current ECMA 5 compliant browsers do not allow assignments to undefined
(and will throw an exception in strict mode).
I prefer typeof someVar === "undefined"
, as you posted, or someVar in window
as Susei suggested.