Hint
Global, function (local), and block scope β each has different variable rules
JavaScript has three types of scope:
window.var inside a function are only accessible inside that function.let or const inside {} are scoped to that block.var globalVar = 'everywhere';
function fn() {
var funcVar = 'function only';
if (true) {
let blockVar = 'block only';
var leaky = 'leaks to fn scope'; // var ignores blocks!
}
console.log(leaky); // β 'leaks to fn scope'
console.log(blockVar); // β ReferenceError
}
console.log(funcVar); // β ReferenceError
The scope chain: When a variable isn't found in the current scope, JS looks up to the outer scope β all the way to global. Inner scopes access outer variables; outer scopes cannot access inner.