Help with understanding function returning

In this chapter, it gives this example. I know I have made a similar post before this, however I cannot wrap my head around how this works. In my thinking, everytime c1() is called, it will run through the function newCounter, set i to 0, then run the anonymous function and add 1 to 0 and return i.

newCounter sets some local variables and returns a regular function, that won’t run newCounter again. Instead, when it’s called, it will increment the variable set in newCounter. This is possible because the returned function is in the same block as the local variable. If it were outside the block, it wouldn’t be able to access the local variable.

1 Like