Closures in JavaScript - Interview Question...
What's a Closure?
When you declare a function inside another function, a closure is the new environment created by combining the inner function with references to all variables available to it from outer scopes (this concept of all scopes accessible from a certain area is known as the lexical environment).
In other words, in a closure, all variables accessible to the inner function -- including variables declared outside the function itself -- remain accessible to it, even when that inner function is removed and called in some other context. The inner function remembers all the stuff it has access to at the time of its declaration.
A closure is a feature in JavaScript where an inner function has, access to the outer function`s variables and parameters - a scope chain
* the inner function remembers the environment in which it was created.
The closure had three scope chains :
- It has access to its own scope- variables defined between its curly brackets.
- It has access to the outer function`s variables
- It has access to the global variables.