Multiple Functions, Same Closure
You'll learn: How several inner functions can share access to the exact same captured variables from their outer scope.
When you define multiple functions inside the same outer function, they don't each get their own private copy of the outer variables—they all share access to the same captured environment. This means they can see each other's changes.
How It Works
Think of it like roommates sharing a kitchen. If one roommate uses milk from the fridge and there's less left, the other roommates see that same reduced amount. They're all accessing the same fridge, not separate copies.
function createCounter() {
let count = 0;
function increment() {
count++;
}
function decrement() {
count--;
}
function getCount() {
return count;
}
return { increment, decrement, getCount };
}
const counter = createCounter();
counter.increment();
counter.increment();
counter.decrement();
console.log(counter.getCount()); // 1
Here, increment, decrement, and getCount all close over the same count variable. When increment changes it, getCount sees that change—they're not working with separate copies.
Shared State, Shared Responsibility
This sharing is powerful because it lets you create related functions that coordinate through shared private data. The count variable stays hidden from outside code, but all three methods can interact with it.
const counter2 = createCounter();
counter2.increment();
console.log(counter2.getCount()); // 1
console.log(counter.getCount()); // Still 1 from before
Each call to createCounter() creates a new closure with its own count, but within one closure, all inner functions share that environment.
Key Takeaway: Multiple functions defined in the same scope share access to the same captured variables—they form a single closure over a shared lexical environment.