UNIT JS-C2 12 hours ARCHIVE 2026
Closures & Scope Chain
FEB 16, 2026ASSET JAVASCRIPT
Deep Dive into Closures
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).
Lexical Scoping
In JavaScript, the scope is determined by the position of variables and blocks within the source code. Nested functions have access to variables declared in their outer scope.
function makeCounter() {
let count = 0;
return function() {
return ++count;
};
}
const counter = makeCounter();
console.log(counter()); // 1
console.log(counter()); // 2
In the example above, the inner function "remembers" the count variable even after makeCounter has finished executing.