I thought that I understand closures, but this example made me confused.
function f() {
let x = 1;
const g = () => {
console.log(x);
}
x = 100;
g();
}
f()
I think that the result should be 1
, but it is 100
. I thought that g
in the moment of declaration should take x
that exists in outer scope. But it took x
value after reassignment. Why is it working like this? I know that you could consider it as a duplicate, but that is just how I understood closures, so please do not link other stack posts. I would be delighted to see some explanation.
Source: Ask Javascript Questions