I've been studying closures and callbacks, for the last hours, and I wrote this function to better understand the concepts of closures. I know that in this example, closure is not happening because callback is called inside the same execution context as function one().
function one(callback) {
let a = 1;
let b = 2;
callback()
}
one(function () {
console.log(a);
});
// Uncaught ReferenceError: a is not defined
Even though the callback is executed in the same execution context as function one()
, why can't I access a
with the callback?
Is it because let being scope limited and therefore the callback
cant access it once it reaches to it's execution? Shouldn't the callback
go down the scope chain and find a
?
Thank you