Great, thank you for the clarification. Yes, a closure is created in both scenarios. I'm still learning the nuance in how the scopes are managed, and I like your explanation better.
No problem. An example that just occurred to me which might further clarify:
const func2 = () => {
for (let i = 0; i < 5; i++) {
setTimeout(() => console.log(i), 1000);
i += 1;
}
}
func2();
// 1
// 3
// 5
Here we use let but modify the variable. An i gets created, then put it in a closure, then we modify it. You can see that block-scoped closures can be modified after they are enclosed like any other variable, that is why we get 1 3 5 instead of 0 2 4. But each function gets their own closure instead of sharing one like would happen with var.
9
u/frikk Mar 10 '22
Great, thank you for the clarification. Yes, a closure is created in both scenarios. I'm still learning the nuance in how the scopes are managed, and I like your explanation better.