In Simpler words Closure means that an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned.
In the above example, InnerFunction() can access outerVariable.
Now, as per the definition above, InnerFunction() can access outerVariable even if it will be executed separately. Consider the following example
In the above example, return InnerFunction; returns InnerFunction from OuterFunction when you call OuterFunction(). A variable innerFunc reference the InnerFunction() only, not the OuterFunction(). So now, when you call innerFunc(), it can still access outerVariable which is declared in OuterFunction(). This is called Closure.
One important characteristic of closure is that outer variables can keep their states between multiple calls. Remember, inner function does not keep the separate copy of outer variables but it reference outer variables, that means value of the outer variables will be changed if you change it using inner function. For Example
In the above example, outer function Counter returns the reference of inner function IncreaseCounter(). IncreaseCounter increases the outer variable counter to one. So calling inner function multiple time will increase the counter to one each time.
Closure is valid in multiple levels of inner functions.
As per the closure definition, if inner function access the variables of outer function then only it is called closure.
The following is not a closure.
When to use Closure?