• Articles
  • Tutorials
  • Interview Questions

JavaScript Closure - The Complete Guide

JavaScript Closure - The Complete Guide

JavaScript closures can be a challenging concept for beginners to understand. In essence, a closure is a function that has access to variables defined outside of its local scope. This means that even after the outer function has returned, the inner function can still access those variables. Understanding closures requires prior knowledge of nested functions and returning a function, as these are the building blocks of closures. With closures, you can create more flexible and powerful functions that can manipulate data and maintain state.

Watch this video on Full Stack Web Development Course:

Video Thumbnail

What are Closures in JavaScript?

What are Closures in JavaScript

JavaScript is a powerful and versatile programming language used extensively in web development. One of its unique features is closures, which often confuse beginners.

Let’s take a deeper look at it. Below are a few points that simplify the concept of JavaScript Closures for you.

A closure is an essential concept that allows functions to retain access to variables from the outer scope even after the outer function has finished executing. In simpler terms, a closure is a function that has access to its scope, the scope of the outer function, and the global scope.

The outerFunction defines a variable outerVariable and declares an inner function innerFunction. The inner function innerFunction has access to the outerVariable due to the closure, even after the outerFunction has finished executing. The returned innerFunction is assigned to greetingFunction, which can now be invoked separately.

The concept of closures is also present in various programming languages like Python, Ruby, and Swift. 

Check out these Web Development Courses to get an in-depth understanding of Web Development!

Lexical Scoping in JavaScript

Lexical scoping, also known as static scoping, is a fundamental concept in JavaScript that determines how variable names are resolved at runtime based on their location in the source code. In lexical scoping, the scope of a variable is determined by its position or context in the code structure rather than its runtime behavior.

To understand lexical scoping, consider the following example:

function outerFunction() {
  var outerVariable = 'Hi';
  function innerFunction() {
    console.log(outerVariable);
  }
  innerFunction();
}
outerFunction(); 

Output:

Hi

In the above code example, we have an outer function outerFunction() that defines a variable outerVariable and an inner function innerFunction(). The inner function references the outerVariable and logs its value to the console.

When outerFunction() is invoked, it creates a new execution context with its own variable environment. The outerVariable is defined within this environment and is accessible to any functions declared within outerFunction().

Now, when innerFunction() is called from within outerFunction(), it has lexical access to the variables in its outer scope, which includes the outerVariable. This is because the inner function is defined within the lexical scope of the outer function.

Lexical scoping ensures that variables declared in an outer scope are accessible to inner scopes but not vice versa. 

In other words, inner functions have access to variables from their enclosing scope, but variables declared within an inner function are not accessible from the outer scope.

Ready to level up your JavaScript skills? Dive into our guide on what is callback function in javascript, by mastering their power and application in your code effortlessly.

Get 100% Hike!

Master Most in Demand Skills Now!

JavaScript Closure Example

JavaScript Closure Example

Let’s dive into a simple example to illustrate the concept of closures in JavaScript:

function outerFunction() {
  var outerVariable = 'Hello, ';
  function innerFunction(name) {
    console.log(outerVariable + name);
  }
  return innerFunction;
}
var greetingFunction = outerFunction();
greetingFunction('Intellipaat'); 

Output:

Hello, Intellipaat

In the above example, the outerFunction defines a variable outerVariable and declares an inner function innerFunction. The inner function innerFunction has access to the outerVariable due to the closure, even after the outerFunction has finished executing. The returned innerFunction is assigned to greetingFunction, which can now be invoked separately.

Want a comprehensive list of interview questions? Here are the Full Stack Developer Interview Questions for Freshers!

Closure Function in JavaScript

Closure Function in JavaScript

Closure functions represent a robust concept within JavaScript, enabling the preservation of a function’s lexical environment even when executed beyond its initial scope. Through the capturing and retention of references to outer function variables, closure functions facilitate the persistence of data even after the function’s execution.

Comprehending Closure Functions: Within JavaScript, functions possess the characteristic of being treated as first-class objects, enabling their assignment to variables, passage as arguments to other functions, and return from functions.This special attribute facilitates the creation of closure functions.

A closure is formed when an inner function accesses variables from its outer function, even after the outer function has completed its execution. The inner function “closes over” these variables, hence the name “closure function.”

function outerFunction() {
  var outerVariable = 'I am from the outer function';
  function innerFunction() {
    console.log(outerVariable);
  }
  return innerFunction;
}

var closure = outerFunction();  // Assigning the returned inner function to a variable
closure();  

Outputs:

"I am from the outer function"

In the code example above, outerFunction defines an outer variable named outerVariable and an inner function named innerFunction. The innerFunction accesses the outerVariable, even though it is executed outside the outerFunction. The closure is created when innerFunction is returned and assigned to the variable closure. Subsequently, invoking closure() outputs the value of outerVariable.

Check out our JavaScript interview questions to crack high-paying top-level MNC interviews.

Creating Private Variables with Closures

Creating private variables with closures is a powerful technique in JavaScript that allows you to encapsulate data and create privacy within your code. By leveraging closures, you can define variables that are only accessible within a specific scope, effectively creating private variables.

Here’s an example to illustrate how closures can be used to create private variables:

function createCounter() {
  let count = 0; // Private variable
  function increment() {
    count++;
    console.log(count);
  }
  function decrement() {
    count--;
    console.log(count);
  }
  return {
    increment: increment,
    decrement: decrement
  };
}
const counter = createCounter();
counter.increment(); // Output: 1
counter.increment(); // Output: 2
counter.decrement(); // Output: 1

In this example, the createCounter function returns an object with two methods: increment and decrement. The key concept here is that the count variable is defined within the scope of the createCounter function, making it a private variable. It cannot be directly accessed or modified from outside the createCounter function.

However, the returned object has references to the increment and decrement functions, which have access to the count variable due to closure. These functions can modify the count variable even though it is not directly accessible outside the createCounter function. This ensures data privacy and prevents unintended modification of the count variable from external code.

By exposing only the necessary methods that operate on the private variable, you can control access and enforce encapsulation. Other parts of your code can only interact with the private variable indirectly through these exposed methods.

This pattern of using closures to create private variables is commonly known as the Module Pattern in JavaScript. It allows you to achieve data hiding and encapsulation, leading to more robust and maintainable code.

Advantages of Closure in JavaScript

Advantages of Closure in JavaScript

The use of closures in JavaScript opens up a wide range of possibilities and benefits in programming. Let’s explore some of the key applications and advantages of closures in JavaScript.

  • Encapsulation and Code Modularity: 
    • Closures enable encapsulation by allowing variables and functions to be enclosed within a specific scope. 
    • This promotes code modularity by keeping related code blocks together, preventing naming conflicts, and providing a cleaner code structure. 
    • With closures, you can create self-contained modules or components, making your code more organized, readable, and maintainable.
  • Data Privacy and Security: 
    • Closures play a crucial role in achieving data privacy and security in JavaScript. 
    • By encapsulating variables within closures, you can make them inaccessible from outside the function’s scope. 
    • This prevents unauthorized access or modifications to sensitive data, ensuring the integrity and reliability of your code.
  • Asynchronous Operations and Callbacks:
    • Closures are usually used in handling asynchronous operations, including callbacks and event handlers. 
    • When a closure is created within an asynchronous function, it retains access to the variables it needs even after the function has completed its execution. 
    • This allows you to maintain the necessary context and data for asynchronous callbacks, ensuring correct and reliable operation in scenarios like AJAX requests or timers.
  • Function Factories and Currying:
    • Closures enable the creation of function factories, where a function generates and returns specialized functions with pre-configured parameters or behaviors. 
    • This is useful in scenarios where you need to create multiple functions with similar functionality but different configurations. 
    • Closures also facilitate currying, a technique that involves creating new functions by partially applying arguments to existing functions, resulting in more flexible and reusable code.
  • Memory Efficiency and State Retention:
    • Closures retain access to variables even after their parent function has finished executing. 
    • This can be advantageous when you need to maintain state or store values for later use. 
    • By utilizing closures, you can avoid unnecessary global variables or excessive memory consumption, making your code more efficient and optimized.

Conclusion

JavaScript closures are a powerful concept that can greatly enhance your coding skills. This beginner’s guide has provided a comprehensive understanding of closures, their implementation, and their benefits. By utilizing closures effectively, you can achieve code modularity, data privacy, and improved code organization. Embrace closures as a fundamental tool in your JavaScript development journey, and with practice and exploration, you’ll unlock their full potential. Keep learning, experimenting, and applying closures to your projects to become a proficient JavaScript developer.

Course Schedule

Name Date Details
Python Course 16 Nov 2024(Sat-Sun) Weekend Batch View Details
23 Nov 2024(Sat-Sun) Weekend Batch
30 Nov 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.