Skip to content

Instantly share code, notes, and snippets.

@detj
Last active December 21, 2015 00:28
Show Gist options
  • Save detj/6219977 to your computer and use it in GitHub Desktop.
Save detj/6219977 to your computer and use it in GitHub Desktop.
void in javascript
// void turns any JavaScript expression to the primitive undefined
// Try the following in your browser's console or node REPL
// Outputs: undefined
console.log(void 0);
// Outputs: undefined
console.log(void 4);
// Outputs: undefined
console.log(void true);
// undefined is not a JavaScript keyword, but a property of the global object
// though it can't be changed
// Outputs: undefined
console.log(this.undefined);
// Oututs: undefined
this.undefined = "foo";
console.log(this.undefined);
// undefined can be shadowed inside a function
(function() {
var undefined = "shadowed";
// Outputs: shadowed
console.log(undefined);
})();
// void can be used to compare with the
// undefined primitive always
(function() {
var undefined = "shadowed";
var notdefined;
// Outputs: false
console.log(undefined === notdefined);
// Outputs: true
console.log(void 0 === notdefined);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment