Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

JavaScript closures are created by defining a function inside another function, which allows the inner function to access the outer function's variables, parameters, and any other data in its scope.

When the outer function is called, it creates a new execution context with its own scope chain, and any inner functions declared inside it creates a new scope chain within the outer function's scope chain.

The inner function can access the outer function's variables and parameters because of this nested scope chain. When the outer function finishes executing, its execution context is destroyed but the inner function retains access to the data in its scope, even after the outer function has returned.

This is how closures allow JavaScript developers to create private variables and encapsulate data within functions, as well as create callback functions and maintain state information between function calls.