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.

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.

See also

Sources

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

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings