5
var name = 'John';

console.log(this.name, document.name, window.name, name);

const meme = "Bruce";

console.log(this.meme, document.meme, window.meme, meme);

Output:

John undefined John John
undefined undefined undefined "Bruce"

Is global scope differently defined for var and const? I thought the only difference would be the const is immutable.

Ska
  • 5,481
  • 9
  • 47
  • 67
  • https://medium.com/javascript-scene/javascript-es6-var-let-or-const-ba58b8dcde75 + https://stackoverflow.com/a/40775470/2008111 – caramba Oct 02 '17 at 06:10
  • 1
    One small thing: const is not immutable (for example you can do array.push()), but it’s not rewriteble (you can not reassign variables) – semanser Oct 02 '17 at 06:12
  • `const` does not work on older browsers (e.g. IE10), `var` works. – naivists Oct 02 '17 at 06:12
  • 1
    @caramba that article doesn't appear to have anything to do with OP's question – Phil Oct 02 '17 at 06:13

1 Answers1

0

Yes, window scope and variables defined using var is different from the scope of variables declared using const and let.

See also Is it possible to delete a variable declared using const?, Why does .then() chained to Promise.resolve() allow const declaration to be reassigned?

guest271314
  • 1
  • 10
  • 82
  • 156