01 / Foundation
Understand the concept
A closure is created whenever a function remembers variables from its outer (lexical) scope even after the outer function has finished executing.
Closures are one of the most important JavaScript concepts because they power data privacy, callbacks, event handlers, currying, memoization, and many modern JavaScript libraries.
02 / Implementation
Basic Closure Example
closure.js
function outer() {
let count = 0;
return function () {
count++;
console.log(count);
};
}
const counter = outer();
counter(); // 1
counter(); // 2
counter(); // 304 / Interview