Glossary termProgramming concept

Closure

Also known as: Lexical closure, Function closure

Function bundled with references to variables from the scope where it was created, letting it keep using them after that scope has finished.

A closure is a function together with the environment in which it was defined. When a function is created inside another function, it can refer to the outer function's local variables, and a closure keeps those variables alive so the inner function can keep using them after the outer one has returned. A simple example is a counter factory: a function that sets a count variable to zero and returns another function that increases and reports that count. Each returned counter keeps its own private count, which no other code can reach.

Closures depend on lexical scope and on treating functions as values that can be stored and passed around. The British computer scientist Peter Landin used the term in the 1960s in his work on the mechanical evaluation of expressions, and the Scheme dialect of Lisp, created in the 1970s, made lexical closures a core feature. Closures appear in most widely used languages, including JavaScript, Python, Ruby, Swift and Rust, and Java gained lambda expressions in 2014. To make them work, implementations keep captured variables in memory that outlives the original function call.

Closures are used constantly in practice. Event handlers, callbacks for asynchronous operations and module patterns that hide private state all rely on them, as do many functions passed to map and filter. They offer a lightweight form of encapsulation comparable to objects, since a closure is data bundled with behavior. They also cause characteristic bugs, such as loops whose callbacks all capture the same variable and see only its final value, and memory leaks when long-lived closures hold references to large objects that are no longer needed.

Key points

  • A closure captures variables from its defining scope and keeps them available after that scope ends.
  • Closures require lexical scoping and functions that can be passed around as values.
  • Callbacks, event handlers and private module state commonly rely on closures.
  • Capturing a loop variable or a large object by mistake can cause subtle bugs and memory leaks.

Related entries

Glossary term

Scope

Programming

The region of a program in which a given variable, function or other name is visible and can be referred to.

Glossary term

Function that takes other functions as arguments or returns a function as its result, such as map, filter and reduce.

Glossary term

Programming paradigm that builds software by composing pure functions, treating computation as evaluating expressions rather than changing state.

Technology

JavaScript

Programming

High-level programming language of the web, created at Netscape in 1995, that runs in every major browser and on servers through runtimes like Node.js.

Technology

Lisp

ProgrammingAI

Family of programming languages originated by John McCarthy in 1958, built on symbolic expressions and long associated with AI research.

Glossary term

Programming approach that lets a program start long-running operations, such as network requests, and continue other work until the results arrive.

Glossary term

Memory Leak

Programming

Bug in which a program keeps holding memory it no longer needs, so its memory use grows over time and can degrade or crash the system.

Referenced by

Other entries that discuss or point to Closure.