---
title: "What are JavaScript closures and how do JavaScript closures work?"  
description: "What are JavaScript closures and how do JavaScript closures work?"  
author: "Utpal Vishwas"  
published: 2023-07-07  
updated: 2023-07-10  
canonical: https://www.mindstick.com/forum/159005/what-are-javascript-closures-and-how-do-javascript-closures-work  
category: "javascript"  
tags: ["javascript", "javascript framework"]  
reading_time: 2 minutes  

---

# What are JavaScript closures and how do JavaScript closures work?

What are [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) closures and how do JavaScript closures work?

## Replies

### Reply by Aryan Kumar

In JavaScript, a closure is a function that has access to the variables of its enclosing function, even after the enclosing function has finished executing. This is because a closure is a combination of a function and the lexical environment within which that function was declared. This environment consists of any local variables that were in-scope at the time the closure was created.

Here is an example of how a closure works:

Code snippet

```plaintext
function outerFunc() {
  var name = "John Doe";

  function innerFunc() {
    // The variable name is still in scope here, even though outerFunc() has finished executing.
    console.log("My name is " + name);
  }

  return innerFunc;
}

var closure = outerFunc();
closure(); // "My name is John Doe"
```

In this example, the `innerFunc()` function has access to the `name` variable, even though the `outerFunc()` function has finished executing. This is because the `innerFunc()` function is a closure, and it has access to the lexical environment of the `outerFunc()` function.

Closures are often used to create reusable functions that can access shared data. For example, you could use a closure to create a function that can format a date, regardless of the current date. The closure would have access to the current date, even though the function was defined before the date was known.

Closures can also be used to create private variables. A private variable is a variable that can only be accessed by the function that created it. You can create a private variable by declaring it inside a closure. For example, the following code creates a private variable called `count`:

Code snippet

```plaintext
function outerFunc() {
  var count = 0;

  function innerFunc() {
    // The variable count is only accessible from within innerFunc()
    count++;
    console.log(count);
  }

  return innerFunc;
}

var closure = outerFunc();
closure(); // 1
closure(); // 2
```

In this example, the `count` variable is only accessible from within the `innerFunc()` function. This is because the `count` variable is declared inside the closure.

Closures are a powerful tool that can be used to create reusable and efficient JavaScript code. If you are not familiar with closures, I recommend taking some time to learn more about them. They can be a valuable asset in your JavaScript toolbox.


---

Original Source: https://www.mindstick.com/forum/159005/what-are-javascript-closures-and-how-do-javascript-closures-work

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
