Last active
December 21, 2015 00:28
-
-
Save detj/6219977 to your computer and use it in GitHub Desktop.
void 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
// 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