JavaScript: Why does closure only happen if I assign the return function to a variable?

_counter is the local variable within function counter(). Every time when you call counter() a new _counter will be created.

But var createClosure = counter() only called the function 1 time, that’s why _counter is not newly created every time and ‘remembered’ there (that’s where closure happens)

Leave a Comment