A function should have access to the variables from the scopes which surround that function. A closure is just any function which remains available after the outer scopes have returned.
Following on from the code in the last post, if a reference can be created to each of the function calls, then we can reuse these called function outputs and they could be available after the function calls are completed and returned.
There are a number of techniques which could be employed, for example
passing to set Timeout
returning the function call from the new function
saving the function call to a global variable
Rewriting the code from before
And now what will be happening behind the scenes.
So now the 5 output lines are:
Anytime you come across a function with an input parameter, which is static, i.e. its not expected to take a new value every time its called. Its an opportunity to refactor your code so that the value is stored in a variable from the outer scope.
Due to the way closures work, the inner scope function will always have access to the outer scope variable, even after the outer function returns.