Created
August 16, 2020 19:39
-
-
Save prof3ssorSt3v3/ef132cd60a04ef6311ec8d891ae05a13 to your computer and use it in GitHub Desktop.
Comparing global variables created with var, let, or no keyword and comparing this, global, window, and globalThis
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Document</title> | |
</head> | |
<body> | |
<script> | |
// https://caniuse.com/#feat=mdn-javascript_builtins_globalthis | |
let lll = 'declared with let'; | |
var vvv = 'declared with var'; | |
(function test() { | |
nnn = 'declared without either'; | |
// console.log(this); | |
// console.log(window); | |
// console.log(globalThis); //added in NodeJS 12.4 | |
console.log(lll, globalThis.lll); | |
console.log(vvv, globalThis.vvv); | |
console.log(nnn, globalThis.nnn); | |
})(); | |
</script> | |
</body> | |
</html> |
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
let lll = 'declared with let'; | |
var vvv = 'declared with var'; | |
(function test() { | |
nnn = 'declared without either'; | |
// console.log(this); | |
// console.log(global); | |
// console.log(globalThis); | |
console.log(lll, globalThis.lll); | |
console.log(vvv, globalThis.vvv); | |
console.log(nnn, globalThis.nnn); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment