QamoosTech
Programming FundamentalsFunctions and scopeIntermediate

Closure

PronunciationKLO-zhur

Definition

A closure is a function that remembers and has access to variables in its outer lexical scope, even after that outer function has finished executing.

Where you hear it

In JavaScript interviews, functional programming discussions, and when explaining data privacy in code.

Examples

  • The inner function forms a closure over the counter variable to keep track of the state.
  • We use a closure to create private variables that cannot be modified directly from the outside.
  • The closure keeps the discount rate, so each price function reuses it without a global variable.

Common mistake

Thinking a closure is a special syntax, when it is actually just a natural behavior of functions retaining access to their creation environment.

Say it at work

  • In a meeting or chatLet's use a closure here to keep the count variable private and secure from outside modification.
  • In an email or ticketI updated the implementation to use a closure so the callback retains access to the current configuration.