Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Function

Closures

Reference for JavaScript closures: how a function remembers variables from its outer scope, private state, loop pitfalls with var and let, and memory notes.

A closure is a function together with the variables it can see from the place where it was created. When an inner function uses a variable from an outer function, the engine keeps that variable alive for as long as the inner function can still be called, even after the outer function has returned.

Every function in JavaScript is a closure in this sense; the word usually describes the interesting case where the outer function is already finished but its variables are still in use.

Try it

JS A counter with private state

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Closures in loops

Press Run (or Ctrl + Enter) to see the console output here.

No output.

How closures work

When a function is created, it records the scope it was created in. When it runs, it looks up names first in its own scope, then in that recorded outer scope, and so on up to the global scope. The recorded scope is a live link to the variables, not a copy of their values. If the outer variable changes, the closure sees the new value.

Each call of the outer function creates a fresh scope, so makeCounter() called twice gives two independent counters, as the first example shows. The variable count cannot be reached from outside except through the functions that closed over it, which makes closures the classic way to get private state.

Common uses

  • Factories and configuration: multiplier(3) returns a function with factor fixed.
  • Private state: counters, caches and ID generators without exposing the variable.
  • Callbacks and event handlers: a handler remembers the data that was in scope when you registered it.
  • Partial application, memoization and debouncing: helpers that keep timers or caches between calls.
  • Module pattern: an immediately invoked function returns an API, keeping everything else private (see IIFE).

Usage notes

  • var in loops shares one variable across all iterations, so every closure sees the final value. let creates a new binding per iteration, which is why the second loop prints 0, 1, 2.
  • Closures capture variables, not values. If you want the value at creation time, copy it into a new variable or pass it as an argument.
  • Memory: captured variables stay alive as long as any closure that uses them is reachable. Long-lived closures that capture large objects keep them in memory; remove event listeners and set references to null when you are done.
  • Engines optimize: only variables that a closure really uses need to be kept, but you should not rely on this for memory-sensitive code.
  • Classes and #private fields are the modern alternative for private state; closures are lighter for small cases.
  • this is not closed over in regular functions, only in arrow functions, which take this from the surrounding scope.

Browser support

Closures are part of the core language and work everywhere. Per-iteration let bindings in loops are available in all current browsers.