What Is a Closure in JavaScript? Explained With Examples

A closure is a function that remembers the variables from its outer scope even after that scope has finished executing. Learn how and why with examples.

Published September 16, 2026

A closure is a function that remembers the variables from the place where it was created, even after that place has finished running. Closures are how JavaScript gives you private state, factory functions, memoization and most callbacks.

The concept

When a function is defined, JavaScript attaches a reference to the surrounding scope (its lexical environment). When the function is called later, it looks up variables through that reference, not through whatever scope is calling it.

Because the reference is live, not a copy, the closure sees updates: if the outer variable changes, the inner function reads the new value.

The outer scope can only be reclaimed by the garbage collector once nothing references the closure any more.

Watch the scope stay alive

Each step below happens in order. Notice that count survives after makeCounter() has returned.

  1. makeCounter() runs and creates the variable count = 0
  2. It returns an inner function that references count
  3. makeCounter() has finished, but count is kept alive by the returned function
  4. Every call to counter() reads and updates the same count: 1, 2, 3 …
A closure keeps its birth scope alive.

Practical examples

Private state with a counter

count cannot be touched from outside; the only way to change it is through the returned function.

function makeCounter() {
  let count = 0
  return () => ++count
}
const a = makeCounter()
const b = makeCounter()
a(); a()          // 2
b()               // 1 — separate closure, separate count

Function factories

One function builds many specialised ones, each remembering its own argument.

const multiplyBy = (n) => (x) => x * n
const double = multiplyBy(2)
const triple = multiplyBy(3)
double(5) // 10
triple(5) // 15

Memoization

The cache lives in the closure so every call shares it but nothing else can reach it.

function memo(fn) {
  const cache = new Map()
  return (arg) => {
    if (!cache.has(arg)) cache.set(arg, fn(arg))
    return cache.get(arg)
  }
}
const slowSquare = (n) => n * n
const fast = memo(slowSquare)

The classic loop bug

var is function-scoped, so all callbacks share one i. let creates a new binding per iteration.

for (var i = 0; i < 3; i++) setTimeout(() => console.log(i))   // 3 3 3
for (let j = 0; j < 3; j++) setTimeout(() => console.log(j))   // 0 1 2

Common mistakes

  • Using var in loops that create callbacks, so every closure sees the final value.
  • Holding large objects in a long-lived closure (such as an event listener) and never removing it, which keeps them in memory.
  • Assuming closures copy values — they keep a live reference to the variable.
  • Creating a new closure on every render or call when a single shared function would do.

Key takeaways

  • A closure = a function plus the scope it was created in.
  • It enables private variables, factories and memoization.
  • Prefer let/const in loops to avoid shared-variable bugs.

FAQ

Why do closures matter in loops?

With var all iterations share one variable, so callbacks created in the loop all see its final value. With let every iteration gets its own binding.

Do closures cause memory leaks?

Only when a long-lived closure keeps large objects reachable. Remove listeners and drop references when you are done.

Are closures slower?

The cost is negligible in practice. Engines optimise them well; write clear code first.

More JavaScript articles