File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 1+ # Episode 10 : Closures in JS
2+ ### Important Interview Question
3+
4+ ** Closure :** Function bundled together with its lexical environment/scope.
5+
6+ ```
7+ 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.
8+ eg:
9+
10+ function x() {
11+ var a = 7;
12+ function y() {
13+ console.log(a);
14+ }
15+ return y; // instead of y();
16+ }
17+ var z = x();
18+ console.log(z); // value of z is entire code of function y.
19+
20+ ```
21+
22+ When functions are returned from another fun, they still maintain their lexical scope.
23+ - When y is returned, not only is the fun returned, but the entire closure (fun y + its lexical scope) is returned and put inside z. So when z is used
24+ somewhere else in program, it still remembers var a inside x().
25+
You can’t perform that action at this time.
0 commit comments