Abdo Amin
Apr 1, 2022

--

Hi Minh, I used "cache" or "memory" as a pointer to explain how closure works. Don't let the pointer confuses you.

What happens is that the increment function still has access to counter variable, despite of increment function being executed outside of it's scope.

On each execution, it is updating the counter, but how does it "keep" the value of counter from the previous execution?

So "keeping" the value is the power of closures, which simply means that the function can still access variables from it's birth's scope.

Thanks for reading :)

--

--

Abdo Amin

I love React and anything that has JavaScript and frontend in it.