Created
April 15, 2012 21:07
-
-
Save Gubaer/2394816 to your computer and use it in GitHub Desktop.
undefined in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Alternative 1 - unsafe | |
function isUndefined(val) { | |
// undefined is a global variable, not a language keyword or a constant | |
// somebody could have reassigned it to, say, "foobar"! | |
// | |
return val === undefined; | |
} | |
// Alternative 2 - safe | |
function isUndefined(val) { | |
// the local variable undefined has the value |undefined| (a kind of special | |
// value in the ECMAScript specification) because it isn't initialized. | |
// | |
var undefined | |
return val === undefined; | |
} | |
// Alternative 3 - safe | |
function isUndefined(val) { | |
// void(0) is an expression evaluating to |undefined| | |
// | |
return val === void(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment