Skip to content

Instantly share code, notes, and snippets.

@Gubaer
Created April 15, 2012 21:07
Show Gist options
  • Save Gubaer/2394816 to your computer and use it in GitHub Desktop.
Save Gubaer/2394816 to your computer and use it in GitHub Desktop.
undefined in JavaScript
// 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