Can someone please explain where the let
variables are get declared in global scope. Variables declared with var
key word are stored as properties of window object and can be accessed with this
key word. Why let
variables behave differently.
var myAge = 36;
function printMyAge () {
var myAge = 32;
console.log (myAge);
console.log(this.myAge)
};
/*
printMyAge(); //Will return
32
36
*/
let myAge_1 = 36;
const printMyAge_1 = () => {
let myAge_1 = 32;
console.log (myAge_1);
console.log(this.myAge_1);
};
/*
printMyAge_1(); //Will print
32
undefined
*/
window.myAge
// returns - 36
window.myAge_1
// returns - undefined
Any help kind explanation would be greatly appreciated.
Thanks in advance