Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created August 16, 2020 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/ef132cd60a04ef6311ec8d891ae05a13 to your computer and use it in GitHub Desktop.
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
<!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>
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