Computer ScienceProgrammingMedium

Closure (programming)

Also known as:lexical closurefunction closure

A closure is a function that retains access to the variables of its enclosing lexical scope even after that scope has finished executing. This allows inner functions to "close over" free variables from outer functions, preserving state without using global variables. Closures are fundamental to functional programming, event handling, and creating data privacy in languages like JavaScript, Python, and Go.

Closure Behaviour Across Popular Languages

LanguageClosure SupportCapture StyleCommon Use Case
JavaScriptYesBy referenceEvent handlers, module pattern
PythonYesBy reference (late binding)Decorators, callbacks
GoYesBy referenceGoroutine captures
JavaPartial (lambdas)Effectively final onlyStream API, listeners
CNo (manual only)N/AFunction pointers + structs

Interactive Tools

Codecademy – JavaScript Closures

Open Tool

Brilliant – Functional Programming

Open Tool

MDN Web Docs – Closures

Open Tool
Diagram illustrating first-class functions and closure scoping

Wikimedia Commons, CC BY-SA

Related Terms

From Latin "claudere" (to close/shut). The computing term was formalised by Peter Landin in 1964 when describing SECD machine environments, referring to the "closing" of an expression over its free variables.

closurescopefunctional-programmingjavascriptfirst-class-functions