Skip to main content

Closure

Pronunciation
KLOH-zher
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/closure

In short

A closure is a function that remembers the variables from the scope where it was created, so it can keep using them even after the outer function has returned.

What is a closure?

A closure is created when a function is defined inside another function and uses variables from that outer function. The inner function keeps a reference to those variables, not a copy, so it can still read and update them later, even after the outer function has finished. In JavaScript, every function forms a closure over the scope it was defined in.

Closures work because of lexical scoping, which means a function's access to variables is decided by where it is written in the code, not by where it is called from. When an outer function finishes, its variables would normally be thrown away, but if an inner function still refers to them, they stay alive in memory.

A backpack is a helpful analogy for a closure: when a function is created, it packs up the variables around it and carries them wherever it goes. Closures are used for private state (data that outside code cannot reach directly), function factories, event handlers, and callbacks that need to remember context.

A closure is often confused with a callback. A callback is simply a function passed to another function to be called later, while a closure is about a function remembering its surrounding variables. Many callbacks are also closures, but the two terms describe different ideas.

How it works

  1. A function is made inside another

    makeCounter declares count and defines a small function that uses it.

    let count = 0;
  2. The inner function is returned

    makeCounter returns that function and finishes.

    return () => ++count;
  3. The variable lives on

    Normally count would be gone now, but the returned function still refers to it, so it is kept.

  4. Every call sees it

    Each call to the returned function updates the same count.

    counter(); // 1 counter(); // 2

Often misunderstood

You might think: A closure copies the variable's value when it is made.

In fact: It keeps a reference to the variable itself, not a copy. If the variable changes later, the closure sees the new value, which is why callbacks made in a loop with var all saw the value the loop ended with.

At a glance

A closure: makeCounter() has returned, but the inner function it handed back, counter, keeps a reference to its count variable, so every call to counter() updates the same count, first to 1 and then to 2.makeCounter()has returnedreturnskeeps a referencecount0 → 1 → 2still in memorycounterthe inner functioncounter() → 1counter() → 2
The outer call is over, yet count lives on, because the returned function still refers to it.

Key takeaways

  • A closure is a function plus the variables it captured from its surrounding scope.
  • Captured variables stay alive as long as the closure exists.
  • Closures capture variables by reference, so they always see the latest value.
  • They enable private state, function factories, and callbacks that remember context.

Example

A counter built with a closurejavascript
// makeCounter returns a function that "closes over" count
function makeCounter() {
  let count = 0; // private: not reachable from outside
  return function () {
    count += 1;  // the inner function still has access
    return count;
  };
}

const counter = makeCounter();
counter(); // 1
counter(); // 2 (count was remembered between calls)

Readers ask

Why are closures useful?

Closures let a function keep private state between calls without using global variables. They are the foundation of patterns like counters, memoization (caching results), and event handlers that remember which data they belong to.

What is the difference between a closure and a callback?

A callback is a function you pass to another function so it can be called later. A closure is a function that remembers variables from where it was created; a callback is often a closure, but the terms describe different things.

Can closures cause memory leaks?

They can if a long-lived closure, such as an event listener that is never removed, keeps references to large objects that are no longer needed. Removing unused listeners and avoiding unnecessary captured data prevents this.

Free · once a week

Software and AI, explained once a week

The week's software and AI news with every term explained, plus a term to learn and a quick quiz. A two-minute read.

You confirm by email first, and one click unsubscribes. Privacy

See this week's issue

About this page

Page history

  1. Added "How it works": how a returned function keeps a variable alive, and why it holds a reference, not a copy.

Changes that altered what this page says. Typo fixes are not listed.

Spotted a mistake or something missing on this page?Suggest an edit

How pages are made

More

Settings