# Closure

URL: https://softwaredictionary.org/terms/closure
Category: Programming Fundamentals
Last updated: 2026-09-29
Pronunciation: KLOH-zher

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.

## 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 closure

```javascript
// 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)
```

## Frequently asked questions

**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.

## Sources

- [MDN: Closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures)

---

Software Dictionary: https://softwaredictionary.org/ · https://softwaredictionary.org/llms.txt
