Closure in JavaScript


As a developer, we encounter many weird behaviors when writing code. One such behavior I actually encountered when writing a JavaScript function and came across the concept called Closure in JavaScript. 
I was supposed to create 3 buttons dynamically and attach the onclick event to it that will alert me to some message based on the button clicked. Before getting into my problem, let me explain something with a simple example.
  1. <div id="pnl">  
  2.     <input type="text" id="name" />  
  3.     <input type="button" id="save" value="Save" />  
  4. </div>  
  5. <script type="text/javascript">  
  6.     function Employee(name) {  
  7.         var time = 5;  
  8.         function Manager() {  
  9.             alert(name + ' will attend the meeting at ' + time);  
  10.             time++;  
  11.         }  
  12.         return Manager;  
  13.     }  
  14.     var s = Employee("Pradeep");  
  15. </script>   
Now, in the preceding JavaScript code, I have one inner function with the name Manager with the outer or parent function called Employee. On calling the Employee() function, nothing happened. Let's try to alert.
  1. alert(s);  
It showed me the output as:
It's showing code written within Manager since I returned Manager instead of calling the function. But if I try to call only s(), it will alert me with the message:
Another way of calling a method is Employee(‘Pradeep')() instead of s(). This is called Closure.
  1. Employee(‘Pradeep')();  
You may know that in JavaScript all local variables and functions are properties to a special internal object called the LexicalEnvironment. This LexicalEnvironment in a browser is the window.
The LexicalEnvironment forms a list of properties chaining that executes from inside out, in other words, Manager has access to the Manager function itself along with the variable "time" and the function "Employee" with its parameter "name". The outer function variable and parameter would act as a global variable for an inner function.
  1. //LexicalEnvironment = window = {name:...,Employee:function}  
  2.     function Employee(name) {  
  3.         var time = 5; //LexicalEnvironment = window = {time:5, name:...,Employee:function}  
  4.         function Manager() //LexicalEnvironment = window = {Manager:function}  
  5.         {  
  6.             //LexicalEnvironment = {}  
  7.             alert(name + ' will attend the meeting at ' + time);  
  8.         }  
  9.         return Manager;    }   
Continuing with the same example, if I try to call s() again, it will display the output as:
Pradeep will attend the meeting at 6
How is it possible that when the variable time is declared within Employee() and incremented within the inner function, it wasn't reset when called again?
Actually the answer to this is, the variable time with the latest value is still being referenced by the inner function Manager(). So every time you call s(), time will be incremented.
This concept wherein:
  • A function within a function with an inner function can access the outer function variables and parameters keeping a reference to the outer LexicalEnvironment.
  • The browser keeps the LexicalEnvironment and all its variables in memory until there is an inner function that references it, is called Closure.
Now let's consider a problem where closure is creating a problem for me. I am trying to create 3 buttons and attaching an onclick event to it, where the clicking of a button causes an alert message to be shown. It seems simple, right?
  1. for (i = 1; i <= 3; i++) {  
  2.     btnObj = document.createElement("button");  
  3.     btnObj.textContent = i + ' Star';  
  4.     btnObj.onclick = function () {  
  5.        alert("You have voted " + i + " Star");  
  6.     };  
  7.     document.body.appendChild(btnObj);  
  8. }   
onclick event
As expected, the output will be 3 buttons and on the click of each button, a respective alert is displayed. But no. You will observe that on the clicking of a button it will display a message as:
display message
This is where Closure is relevant. As said before, closure preserves the latest value until the function "onclick" is referenced even though the loop has completed, the last value is stored in i is 4.This is the reason it will display weird output.
Oh, how to solve this issue? The same closure will help me to solve it. Let's see. :)
  1. for (i = 1; i <= 3; i++) {  
  2.    btnObj = document.createElement("button");  
  3.    btnObj.textContent = i + ' Star';  
  4.    btnObj.onclick = (function (k) {  
  5.         return function () {  
  6.              alert("You have voted " + k + " Star");  
  7.         };  
  9.    } (i));  
  10.      document.body.appendChild(btnObj);  
  11. }   
Notice that this time our onclick function is actually being invoked automatically and the function actually returns another function. This allows us to pass in the value of i into that function (that is using the variable k in the alert) since it was when the function was created. Now we will have the correct value for the button when it is clicked.
  1. Closure in jQuery  
  2. var btnlist = $("button");  
  3.     btnlist.each(function (index, obj) {  
  4.         $(obj).click(function () {  
  5.             alert("Clicked " + $(this).text() + " having index-" + (index + 1) + " of " + btnlist.length + " buttons");  
  6.         });  
  7.     });  
In the above example, as you can observe the callback function of the click event is able to access the parameter "obj" and "index" along with the btnlist array. This is how Closure is used even in jQuery.


The Closure mainly has three scope chains:
  1. It has access to its own scope (LexicalEnvironment),
  2. It has access to the outer function's variables and parameters
  3. It has access to global variables. This is by default known to everyone.
Closures are extensively used in the jQuery library, Node.js, asynchronous architecture. I hope you like this article.