Skip to content

Commit 6a4799b

Browse files
Create Ep10.md
1 parent 29b6eef commit 6a4799b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Notes/Ep10.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+

0 commit comments

Comments
 (0)