Closures in JavaScript

Closures In JavaScript

 
This article explains how to use closures in JavaScript. Basically, a closure is a local variable that will be executed after the specific function has returned. A Clouse provides us a free environment that the outer function can easily access inner functions and inner variables without any scope restrictions. So we will first explain the types of scope. 
 

Local Scope

 
In Local Scope, we can't access a variable defined in a function. In other words, if we assign a variable then that is defined and accessible for a certain part of the code as in the following.
 
Example
  1.  <html>  
  2.   <head>  
  3.     <script language="javascript" type="text/javascript">  
  4.         function Local() {  
  5.             var s = "my name is Mahak";  
  6.             alert(s);  
  7.         }  
  8.         alert(s);    //Throws an error  
  9.      </script>  
  10.   </head>  
  11.   <body onload="Local()">   
  12.   </body>  
  13. </html> 
In this example, we define a variable s and it can be accessible in the function Local(). So if we can use it outside the function then it returns an error or it is not accessible.
 
Note: We can call the Local Variable as a Global Variable when we declare it without var keyword. The Local Variable is treated as a Global Variable when we call the specific function at least one time as in the following.
 
Example
  1.  <html>  
  2.   <head>  
  3.     <script type="text/javascript" language="JavaScript">  
  4.         function scope() {  
  5.             var lv1 = 'My First Local Variable';  
  6.             lv2 = 'My Second Local Variable';  //delared without var keyword  
  7.             document.writeln('Local Variables:<br /><br />');  
  8.             document.writeln(lv1 + '<br />');  
  9.             document.writeln(lv2 + '<br />' + '<br />');  
  10.         }  
  11.         scope();  
  12.         alert(lv2);  
  13. </script>   
  14.   </head>  
  15.   <body onload="scope()">   
  16.   </body>  
  17. </html> 
The output will be:
 
JavaScript
 

Global Scope

 
Global means we can use a variable or a function anywhere without any restrictions. We can also declare it without a var keyword.
 
Example
  1. <html>  
  2.  <head>  
  3.    <script type="text/javascript" language="JavaScript">   
  4.        var gv1 = 'My First Global Variable';  
  5.        gv2 = 'My Second Global Variable'//delared without var keyword   
  6.        function scope() {  
  7.            var lv1 = 'My First Local Variable';  
  8.            lv2 = 'My Second Local Variable'//delared without var keyword   
  9.            document.writeln('Global Variables:<br /><br />');  
  10.            document.writeln(gv1 + '<br />');  
  11.            document.writeln(gv2 + '<br />' + '<br />');   
  12.            document.writeln('Local Variables:<br /><br />');  
  13.            document.writeln(lv1 + '<br />');  
  14.            document.writeln(lv2 + '<br />' + '<br />');  
  15.        }   
  16. lt;/script>   
  17.  </head>  
  18.  <body onload="scope()">   
  19.  </body>  
  20. </html> 
The output will be:
 
Output-JavaScript
 

Closures 

 
A closure is a local variable that will be executed after the specific function has returned. A Closure provides us a free environment for the outer function to easily access inner functions and inner variables without any scope restrictions. 
 
Example
  1. <html>  
  2.   <head>  
  3.     <script type="text/javascript" language="JavaScript">   
  4.         function ExClosure(a, ref1) {   
  5.             var v1 = a;  
  6.             var ref = ref1;   
  7.             return function (v2) {  
  8.                 v1 += v2;  
  9.                 alert("Add:" + v1)  
  10.                 alert("Reference Value:" + ref.cv);  
  11.             }  
  12.         }  
  13.         myclosure = ExClosure(10, { cv: 'My First Closure' });  
  14.         myclosure(5);  
  15. </script>   
  16.   </head>  
  17.   <body onload="ExClosure(b,ref)">  
  18.   </body>  
  19. </html> 
In this example, when the ExClosure function is called, it returns a function. The function remembers the value of a (10) in the form of v1, it means the myclosure will add 10 together with 5 and return 15 as an alert, and the next alert returns the Reference value (Reference Value: My First Closure).
 
The output will be:
 
Output1-JavaScript
 
Output2-JavaScript
 
If we use:
  1. Myclosure1=ExClosure(10,{cv:'My Second Closure'});  
  2. Myclosure1(20); 
Output
 
Output3-JavaScript
 
Output4-JavaScript