Closures in JavaScript

Hemanth Raju
JavaScript in Plain English
3 min readNov 14, 2023

--

Closures in Javascript

A closure is a JavaScript feature that allows inner functions to access the outer scope of a function. Closures help in binding a function to its outer boundary and is created automatically whenever a function is created.

  • An important thing about closures is that “Closures have access to variables in their lexical scope, even after the outer function has finished executing.”

Here’s an example to illustrate closures:

  • To understand closures much better, will take an example and see what happens with closure and without creating closure.

Without Closures:

// Without closure
function outerFunction() {
let outerVariable = "I am from the outer function";

function innerFunction() {
// innerFunction cannot access outerVariable
console.log(outerVariable); // ReferenceError: outerVariable is not defined
}

innerFunction();
}

outerFunction();

In this example, innerFunction cannot access outerVariable because it is not defined within the scope of innerFunction.

With Closures:

// With closure
function outerFunction() {
let outerVariable = "I am from the outer function";

function innerFunction() {
// innerFunction has access to outerVariable due to closure
console.log(outerVariable); // Outputs: I am from the outer function
}

return innerFunction; // Returns the inner function, creating a closure
}

const closureExample = outerFunction();
closureExample(); // Executes innerFunction and retains access to outerVariable

In this example, innerFunction is returned from outerFunction, creating a closure. The returned function, closureExample, retains access to the outerVariable even though outerFunction has finished executing. When closureExample is called, it can still access and use outerVariable due to the closure.

Hope the above example helps to understand why we need closures and the necessity of using closures.

Let’s see some advantages and disadvantages of closures.

Advantages of Closures:

  • Encapsulation: Closures create private scopes, preventing unintended access to variables.
  • Data Persistence: Variables in closures persist between function calls, retaining state.
  • Factory Functions: Closures facilitate the creation of factory functions for generating specialized functions.
  • Callback Functions: Closures enable callbacks to access variables from their defining scope.
  • Partial Application and Currying: Closures are essential for creating functions with partial application or currying.
  • Module Pattern: Closures are key to implementing the Module Pattern for code organization.
  • Memory Efficiency: Closures can improve memory efficiency by sharing variables among inner functions.

Disadvantages of Closures:

  • Memory Consumption: Closures may lead to increased memory usage and potential memory leaks.
  • Complexity and Readability: Improper use of closures can result in complex and less readable code.
  • Potential for Stale Data: Closures capturing variables by reference might hold references to stale data.
  • Performance Overheads: Closures may introduce some performance overhead compared to simpler function calls.
  • Debugging Challenges: Debugging closures, especially nested ones, can be challenging.
  • Global Namespace Pollution: Improper use of closures may contribute to global namespace pollution.
  • Security Concerns: Improperly managed closures might expose unintended access to private data, posing security risks.

In short, Closures are powerful as they enable functions to have persistent access to the variables from their containing scope, providing a way to create private variables and maintain state across function calls. This is often used in scenarios like creating private data or implementing higher-order functions in JavaScript.

Thanks for reading📖! I hope you enjoyed😀 reading this article.

You can subscribe here for regular articles😇.

Keep smiling😁!

Have a nice day!

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

--

--