Skip to content
uencoder

Closure

A closure is the binding of a function with its defining environment, letting an inner function access the outer function's variables even after the outer function has returned.

Purpose

Enables data privacy, state retention, and function factories — a core technique of functional programming.

Mechanism

A function captures its lexical environment (variable bindings) at creation. Even after the outer function returns, the closure still holds a reference to the environment. Uses include private variables, callback state, currying, and the module pattern. Beware of accidentally capturing loop variables.

Use cases

  • Functional programming
  • Data privacy
  • Callback state

Related terms

References