Course contentsShow
← JavaScript
Lesson 506 of 2,72313. ClosuresFree lesson

Closures Don't Copy the Scope

Closures maintain a live reference to the outer scope; changes to captured variables are visible to all closures sharing that scope.

Closures Don't Copy the Scope

You'll learn: Closures keep a live link to their outer scope—they don't take a snapshot.

The Core Idea

When a closure captures a variable from its outer scope, it doesn't make a private copy of that variable's value. Instead, it maintains a reference to the actual variable itself. This means if the outer variable changes, all closures that captured it will see the updated value.

Think of it like roommates sharing a whiteboard. They don't each take a photo of what's written—they all look at the same board. When someone erases and rewrites something, everyone sees the new message.

How This Works in Practice

function createCounters() {
  let count = 0;

  function increment() {
    count++;
    console.log('Incremented:', count);
  }

  function getCount() {
    console.log('Current count:', count);
  }

  return { increment, getCount };
}

const counters = createCounters();
counters.getCount();   // Current count: 0
counters.increment();  // Incremented: 1
counters.getCount();   // Current count: 1
counters.increment();  // Incremented: 2
counters.getCount();   // Current count: 2

Both increment and getCount share the same count variable. When increment changes it, getCount sees the change immediately. The scope wasn't copied—it's a living, shared environment.

This is why the loop closure trap happens: all iterations reference the same changing variable, not separate frozen copies.

Key Takeaway: Closures maintain live references to outer scope variables, not snapshots—changes made by one closure are visible to all others sharing that scope.