VOOZH about

URL: https://www.geeksforgeeks.org/javascript/closure-in-javascript/

⇱ Closure in JavaScript - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Closure in JavaScript

Last Updated : 17 Jun, 2026

A closure is the combination of a function and its lexical environment, allowing the function to access variables from its outer scope even after the outer function has finished executing.

  • Retains access to outer function variables.
  • Preserves the lexical scope.
  • Allows data encapsulation and privacy.
  • Commonly used in callbacks and asynchronous code.
  • The function inner() forms a closure by retaining access to outerVar, which is a variable in the scope of outer().
  • Even though outer() has completed execution, inner() still has access to outerVar due to the closure.

Lexical Scoping

Closures rely on lexical scoping, which means a function’s scope is determined by where it is defined, not where it is executed.

  • A function retains access to the scope where it was defined.
  • Inner functions can access outer function variables.
  • Enables closures to “remember” their environment.

Private Variables

Closures allow a function to keep variables private and accessible only within that function, which is commonly used in modules to protect data from being accessed or modified by other parts of the program.

  • Helps achieve data encapsulation
  • Creates private variables
  • Prevents accidental data modification

Closures and IIFE

IIFEs (Immediately Invoked Function Expressions) use closures to encapsulate data within a function, keeping it private and preventing access from the outside, which helps create self-contained modules.

  • Data is scoped to the IIFE.
  • Prevents global namespace pollution.
  • Uses closures for data privacy.
  • Useful for creating modular code.

Closure and setTimeout

Closures are helpful in asynchronous programming because they preserve access to variables even after a function has finished executing, which is essential for delayed operations like timers or server requests.

  • Retains state for delayed execution.
  • Works well with callbacks and promises.
  • Useful with timers (setTimeout, setInterval).
  • Helps manage async data flow.

Closures with this keyword

Closures can be confusing with the this keyword because this is determined by how a function is called, not where it is defined, so inside a closure it may not refer to the expected object.

  • this is determined by how a function is called, not by closures.
  • Arrow functions inherit this from their surrounding scope.
  • Use bind(), call(), or arrow functions when needed.

Common Pitfalls

  • Memory Leaks: Excessive use of closures may retain unnecessary references to variables, causing memory issues.
  • Performance Overhead: Overusing closures might lead to larger memory usage due to retained scopes.
  • Issues Inside Loops: Using var inside loops may cause unexpected behavior because all closures share the same variable.

All callbacks share the same i variable created with var, so the output is 4 4 4. Using let creates a new binding for each iteration.

Also Read:

Comment