Closures in JavaScript

A closure can be defined as a JavaScript feature in which the inner function has access to the outer function variable. In JavaScript, every time a closure is created with the creation of a function.

The closure has three scope chains listed as follows:

  • Access to its own scope.
  • Access to the variables of the outer function.
  • Access to the global variables.

Let’s understand the closure by using an example.

<!DOCTYPE html>  
<html>  
<head>  
<script>  
function fun()  
{  
var a = 4; // 'a' is the local variable, created by the fun()  
function innerfun() // the innerfun() is the inner function, or a closure  
{  
return a;  
}  
return innerfun;  
}  
var output = fun();  
document.write(output());  
document.write(" ");  
document.write(output());  
</script>  
</head>  
<body>  
  
</body>  
</html>  

Output

4 4

In the above program we have two functions: fun() and innerfun(). The function fun() creates the local variable a and the function innerfun(). The inner function innerfun() is only present in the body of fun(). The inner function can access the outer function’s variable, so the function innerfun() can access the variable ‘a’, which is declared and defined in fun().

This is the closure in action in which the inner function can have access to the global variables and outer function variables.

In the output, the code will display the value of the variable ‘a’, defined in the parent function.

Now, there is another example in which we will use the parameterized function

Example2

<!DOCTYPE html>  
<html>  
<head>  
<script>  
function fun(a)  
{  
function innerfun(b){  
return a*b;  
}  
return innerfun;  
}  
var output = fun(4);  
document.write(output(4));  
document.write(" ");  
document.write(output(5));  
</script>  
</head>  
<body>  
  
</body>  
</html>  

Output

16 20

In the above program there are two parameterized functions: fun() and innerfun(). The function fun() has a parameter a, and the function innerfun() has the parameter b. The function fun() returns a function innerfun() which takes an argument and returns the multiplication of a and b. In the program, the output is the closure.

Closures are one of the slightly difficult to understand concept of JavaScript, but try to practice the closure in different scenarios like to create callbacks, getters/setter.

Related Posts

What are custom events in JavaScript?

Custom events are the events that allow you to decouple the code you want to run after a specific piece of code runs. There are various in-built events…

How to use nested for loop in JavaScript?

We use the for loop statement of JavaScript for repeating a set of statements inside the loop body a specified number of times. A nested for loop, as the…

What are the basic rules for JavaScript parameters?

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as…

How to stop refreshing the page on submit in JavaScript?

Using event.preventDefault() to stop page refresh on form submit In this section, we will see how to use event.preventDefault() to stop page refresh on form submission. The event.preventDefault() restricts the default…

Target a Window Using JavaScript or HTML

TARGET attribute of HTML A link’s opening named frame or window is specified using the Target attribute of the <a> anchor tag. The concluding </a> tag in…

What is the role of deferred scripts in JavaScript?

Since JavaScript is a loosely typed language, you are not required to correctly predict the kind of data that will be kept in a variable. Depending on the information…