What is a closure?
While there are tons of different resources on the web suggesting what defines a closure, there is one thing in common with every definition. It is that at some point, there is a function that accesses a variable outside of its own scope. And that is all a closure really is. It’s a very simple concept, but that does not mean that it is at all an easy one, and is one of the most common causes of bugs.
A closure in its simplest form would look something like this:
var x = 20;
function closure() {
return x;
}
It is important to note that a closure does not have anything to do with execution context, and everything to do with lexical scope. In other words, the number of closures in a piece of code can be determined just by looking at it before it ever runs.