---
title: "What is meant by Closure in Javascript?Give an example."  
description: "What is meant by Closure in Javascript?Give an example."  
author: "Amrita Bhattacharjee"  
published: 2023-03-28  
updated: 2023-03-28  
canonical: https://www.mindstick.com/forum/157611/what-is-meant-by-closure-in-javascript-give-an-example  
category: "javascript"  
tags: ["javascript", "javascript object"]  
reading_time: 2 minutes  

---

# What is meant by Closure in Javascript?Give an example.

Closure refers to a feature which is used to allow any function for remembering and accessing the lexical scope of it in time of [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) of it outside of that scope as well. This means that any function which can be used to maintain the access to the [variables](https://www.mindstick.com/articles/715/php-variables) [as well as](https://www.mindstick.com/interview/2481/can-you-write-a-java-class-that-could-be-used-both-as-an-applet-as-well-as-an-application) [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp) from the parent function and after the complete execution of that parent [function as](https://www.mindstick.com/forum/212/can-be-declare-a-static-function-as-virtual) well.

An example of closure in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) is as follows:

```javascript
function outerFunction() {
  var outerVariable = "I am outside!";

  function innerFunction() {
    var innerVariable = "I am inside!";
    console.log(outerVariable);
    console.log(innerVariable);
  }

  return innerFunction;
}

var innerFunc = outerFunction();
innerFunc();
```

Here in the mentioned example the outerFunction is used to return the innerFunction as a output. When the call of outerFunction [takes place](https://yourviews.mindstick.com/view/81718/pranab-mukherjee-death-how-last-rites-of-former-president-of-india-takes-place) then it creates [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) [called as](https://www.mindstick.com/forum/471/why-is-white-box-testing-called-as-glass-box-testing) outerVariable and a specific function called as innerFunction. This innerFunction is used to create innerVariable.

The execution of innerFunction logs the value of outerVariable as well as innerVariable to the console. The innerFunction is used to maintain the access to the outerVariable because of closure.

## Replies

### Reply by Amrita Bhattacharjee

Closure refers to a feature which is used to allow any [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) for remembering and accessing the lexical scope of it in time of execution of it outside of that scope as well. This means that any function which can be used to maintain the access to the variables as well as parameters from the parent function and after the complete execution of that parent function as well.

An example of closure in JavaScript is as follows:

```javascript
function outerFunction() {
  var outerVariable = "I am outside!";

  function innerFunction() {
    var innerVariable = "I am inside!";
    console.log(outerVariable);
    console.log(innerVariable);
  }

  return innerFunction;
}

var innerFunc = outerFunction();
innerFunc();
```

Here in the mentioned example the outerFunction is used to return the innerFunction as a output. When the call of outerFunction takes place then it creates variable called as outerVariable and a specific function called as innerFunction. This innerFunction is used to create innerVariable.

The execution of innerFunction logs the value of outerVariable as well as innerVariable to the console. The innerFunction is used to maintain the access to the outerVariable because of closure.


---

Original Source: https://www.mindstick.com/forum/157611/what-is-meant-by-closure-in-javascript-give-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
