Javascript global scope with constant vs var

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 a global scope defined for var and const? I thought the only difference would be const immutable.

+5
source share
1 answer

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

See also. Is it possible to delete a variable declared with const? Why .then () is riveted to Promise.resolve () to allow the declaration of a constant?

0
source

Source: https://habr.com/ru/post/1272271/


All Articles