Closure
Noun · Development
Definitions
Closure is a programming concept where a function retains access to variables from its enclosing scope even after that outer function has finished executing. The inner function closes over the variables it references, capturing them in a persistent environment rather than just their values at the time of creation. Closures are fundamental to functional programming and appear in nearly every modern language including JavaScript, Python, Ruby, Swift, and Rust. Common use cases include data privacy (creating private variables that only the closure can access), callback functions that need to remember context, factory functions that generate specialized functions, and event handlers in user interfaces. In JavaScript, closures are the mechanism behind module patterns and are essential for asynchronous programming with promises and callbacks. Understanding closures is considered a key milestone in a programmer's development, as they enable powerful patterns for encapsulation and state management.
In plain English: A function that remembers the variables from the place where it was created, even when it runs somewhere else entirely.
Example: "Every time I think I understand closures, I find a new edge case that proves I don't."
Etymology
- 1964
- Peter Landin introduces the concept of a closure in his description of the SECD machine for evaluating lambda expressions.
- 1975
- Scheme implements closures as first-class values, demonstrating their power for functional programming.
- 1995
- JavaScript inherits closures from Scheme via Self. They become the primary mechanism for encapsulation in JS.
- 2010s
- Closures are standard in nearly all modern languages (Python, Ruby, Swift, Rust, C++11). Lambda expressions popularize them further.
Origin Story
The Function That Remembers Where It Came From
A closure is a function bundled together with references to its surrounding state (the lexical environment), allowing the function to access variables from its enclosing scope even after that scope has finished executing. The concept was first described by Peter Landin in his 1964 paper 'The Mechanical Evaluation of Expressions,' where he defined the SECD machine for evaluating lambda calculus expressions. Landin needed a mechanism to capture the environment in which a function was defined so it could be evaluated later in a different context. He called this combination of a function and its environment a 'closure' because it 'closes over' the free variables in the function body. The concept was implemented in Scheme (1975), designed by Guy Steele and Gerald Jay Sussman at MIT, which made closures a core feature. For decades, closures were considered an advanced functional programming concept. JavaScript brought them to the mainstream: because JavaScript functions are first-class values and have lexical scoping, closures arise naturally and are used constantly for callbacks, event handlers, and data privacy patterns. Today closures are supported in nearly every modern programming language, including Python, Ruby, Swift, Rust, Go, and Java (since version 8).
Coined by: Peter Landin
Context: Paper on mechanical evaluation of expressions, 1964
Fun fact: Before JavaScript popularized closures, many programmers considered them an obscure academic concept. Douglas Crockford's influential 2001 essay 'JavaScript: The World's Most Misunderstood Language' argued that closures were one of JavaScript's best features hiding in plain sight.
Related Terms
- Algebraic Data Type
- BuckleScript
- Bundler
- Category
- Computation Expression
- Cons
- Continuation
- Currying
- Dynamic Typing
- Export
- Fable
- Higher-Order Function
- Lambda
- List Comprehension
- Mapping
- Namespace
- Upvar
- JavaScript Engine
- Match Expression
- Partial Function
- PostMessage
- Property Accessor
- Scan
- Side Effect
- State Reducer
- Static Variable
- Stimulus
- Symbol
- Type Signature
- Typed Array
- Web Animation API
- Web Audio API