let a=5;
var b=6;
console.log(this.a); //why we get undefined if we run this in global context.
console.log(this.b); //output:6`let a=5;
why we get undefined if we console.log(this.a);
CodePudding user response:
From MDN Web Docs:
"unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope. The other difference between var and let is that the latter is initialized to a value only when a parser evaluates it"
Can read more about it here: MDN - let
CodePudding user response:
Because this
seems not to be inside a class or object in your case, it can only reference an object or class it belongs to.