-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10. Closures .js
More file actions
21 lines (21 loc) · 772 Bytes
/
10. Closures .js
File metadata and controls
21 lines (21 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Closures ::
/*
* A closure is a function that retains access to its lexical scope,
* even when the function is executed outside that scope.
* Closures are created every time a function is defined,
* and they allow for private variables and encapsulation.
* Example:
* function outerFunction() {
* let outerVariable = "I'm from outer scope";
* function innerFunction() {
* console.log(outerVariable);
* }
* return innerFunction;
* }
* const closure = outerFunction();
* closure(); // Output: I'm from outer scope
* In this example, `innerFunction` forms a closure that captures
* the `outerVariable` from its lexical scope.
* So basically, closures are powerful for creating private state
* and maintaining context in asynchronous code.
*/