-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClosure.js
More file actions
23 lines (17 loc) · 1.06 KB
/
Closure.js
File metadata and controls
23 lines (17 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Closure
// A closure occurs when you return a function from another function, and the returned inner function remembers its lexical scope even when the outer function has completed execution.
function outerFunction() {
let count = 0; // This variable is within the lexical scope of outerFunction.
return function innerFunction() {
console.log(count); // innerFunction has access to the 'count' variable even though outerFunction has finished executing.
count++;
};
}
const innerFunction = outerFunction(); // Calling outerFunction returns innerFunction, creating a closure.
innerFunction(); // Outputs 0, as it starts with the initial value of 'count'.
innerFunction(); // Outputs 1, 'count' is remembered between function calls.
innerFunction(); // Outputs 2, 'count' continues to increment.
const innerFunction2 = outerFunction(); // Creating a new instance of the closure with its own 'count' variable.
innerFunction2(); // Outputs 0, independent of the 'count' in the first closure.
innerFunction2(); // Outputs 1.
innerFunction2(); // Outputs 2.