Closure : Function bundled together with its lexical environment/scope.
JS is a weird language. You can pass functions as parameters to another function, assign a variable to an entire function, or even return a function.
e.g.:
function x() {
var a = 7;
function y() {
console.log(a);
}
return y; // instead of y();
}
var z = x();
console.log(z); // value of z is entire code of function y.When functions are returned from another function, they still maintain their lexical scope.
- When
yis returned, not only is the function returned but the entire closure (function y + its lexical scope) is returned and put insidez. So whenzis used somewhere else in program, it still remembers variableainsidex() - Closure is a very powerful concept of JS, just because this function remembers things even if they are not in their lexical scope
function x() {
var a = 7;
function y() {
console.log(a);
}
a = 100;
return y;
}
var z = x();
z();OUTPUT:
100
function w() {
var b = 900;
function x() {
var a = 7;
function y() {
console.log(a, b);
}
y();
}
x();
}
var z = w();
z();OUTPUT:
7 900
- Module Design Pattern,
- Currying,
- Functions like once(fxn that can be run only once),
- memoize,
- maintaining state in async world,
- setTimeout,
- iterators
- ...