JavaScript Closure In Action

Who is this article intended for?

All folks who are working with JavaScript, learning JavaScript or have been using fancy JavaScript frameworks and platforms like AngularJS, NodeJS or even as basic as jQuery or if you plan to start writing JavaScript and transitioning from an object oriented languages like C# or you are a reader and like reading non-fiction articles.

JavaScript

JavaScript is the most forgiving language I have ever come across. Everything just seems to work, well this can be dangerous, sometimes - if not always; we might end up writing something that works but may be very inefficient or even worst, after a long day’s work, the script might seem perfect but the output may be equally bizarre. I agree that JavaScript may be unintuitive, occasionally, even counterintuitive. Let’s look at following example to see what I mean.

JavaScript 
  1. 'use strict'    
  2. for (var i = 0; i < 10; i++)            
  3. {     
  4.    console.log('"i" is equal to ' + i);   
  5. }    
  6. //Code 1.0  
Can you guess what output will it produce? You guessed it right.

It will produce following output, 

  1. "i" is equal to 0   
  2. "i" is equal to 1   
  3. "i" is equal to 2   
  4. "i" is equal to 3   
  5. "i" is equal to 4   
  6. "i" is equal to 5   
  7. "i" is equal to 6   
  8. "i" is equal to 7   
  9. "i" is equal to 8   
  10. "i" is equal to 9    
  11.   
  12. //Output 1.0  
Pretty predictable right? Now lets add a tiny bit of complexity, we want to add a delay between the output prints. Meaning, we want to print 0 instantly, 1 after 1 sec, 2 after 2 sec and so on and so forth. This isn’t much of a challenge, right? Following your intuitions you might write something similar.

JavaScript
  1. 'use strict'    
  2. for (var i = 0; i < 10; i++) {     
  3.    setTimeout(function () {    
  4.       console.log('"i" is equal to ' + i + ' (after ' + i + ' secs)');     
  5.    }, (1000 * i));   
  6. }    
  7.   
  8. //Code 1.1  

And you would be expecting following output, 

  1. "i" is equal to 0 (after 0 secs)   
  2. "i" is equal to 1 (after 1 secs)   
  3. "i" is equal to 2 (after 2 secs)   
  4. "i" is equal to 3 (after 3 secs)   
  5. "i" is equal to 4 (after 4 secs)   
  6. "i" is equal to 5 (after 5 secs)   
  7. "i" is equal to 6 (after 6 secs)   
  8. "i" is equal to 7 (after 7 secs)   
  9. "i" is equal to 8 (after 8 secs)   
  10. "i" is equal to 9 (after 9 secs)    
  11.   
  12. //Assumed Output 1.1  
If you though so, I won’t blame you.

Above code will actually produce following output.

Bizarre right? Didn’t I say that javascript can be counterintuitive sometimes!

What just happened? – Well, Meet Closure!

What you just encountered is called closure. Well, Don’t be rude, say hello! Closure is a friend if understood and used wisely but a fearful enemy if left unnoticed.

Closure enables functions to remember its scope of execution. Meaning, it makes functions remember the variables and other functions it refers to. Closure makes the referred variables and functions available, even after the scope is exited, latest values are reflected. Simplest example of closure is following hello world example,

JavaScript
  1. 'use strict'    
  2.   
  3. //Following function returns a function which prints - Hello `name`!   
  4. function sayHelloTo(name){      
  5.    return function (){    
  6.          //This function "remembers" the name -- thanks to closure.  
  7.          console.log('Hello ' + name + '!');      
  8.    }   
  9. }    
  10.   
  11. var helloWorld = sayHelloTo('World');   
  12. /*    The closure enables the child function "remember" the "name" variable    even after the parent scope has exited. */    
  13.   
  14. //Prints out "Hello World!" in the console.   
  15. helloWorld();    
  16.   
  17. //Code 1.2  
produces
  1. Hello World!  //output 1.2   

Pretty neat right? And this is what happened with our first example, closure. Little bit cryptic, ain’t it? Let me elaborate, javascript runs on a single thread, so it queues up tasks that needs differed execution, remember setTimeout - we used it in our first example? So what happened with the example is first the for loop executes and iterates for 10 times with value of i = 0 to 9, with each iteration it executes setTimeout block with appropriate value of i, meaning for i = 0 => setTimeout(<function>,0) for i = 1 => setTimeout(<function>,1000) and for i = 9 => setTimeout(<function>,9000). Now setTimeout is executed but the <function> goes through deferred execution, so the first <function> is executed only after the for loop is executed completely. So at the time when <function> executes, the for loop has already exited and final value of iis equal to 10. Now due to closure, <function> remembers the i and fetches the latest value, which is 10 and hence the cryptic output. What we learned is that the value of i is NOT COPIED for each iteration instead the reference is remembered.

How do I make it work then?

There is more than one way to get what we want. First lets look at IIFE.

Option 1: IIFE

IIFE (immediately-invoked function expression) are function expressions which are executed as soon as they are defined. For example

JavaScript 
  1. 'use strict'   
  2.   
  3. //The IIFE   
  4. (function (arguments) {     
  5.    /* Awesomeness goes here */   
  6. })();    
  7.   
  8. //Code 1.3  
As you can see that the function is invoked as soon as it is defined. So we can rewrite Code 1.1 as,

JavaScript

  1. 'use strict'    
  2.   
  3. for (var i = 0; i < 10; i++) {  
  4.    /*      IIFE with a parameter i, this i parameter is different then i in 
  5.            the for loop. IIFE forces a new execution scope to be created 
  6.            and current *value* of the i in for loop is *copied* to the new 
  7.            scope    
  8.   */  
  9.    (function (i) {  
  10.       //i has *value* i (of for loop) at the time of invocation       
  11.       setTimeout(function () {  
  12.           console.log('"i" is equal to ' + i + ' (after ' + i + ' secs)');       
  13.       }, (1000 * i));  
  14.    })(i); //i of the for loop is passed as parameter, forcing new scope to be     
  15.           //created with current copy of i.   
  16. }   
  17.   
  18. //Code 1.1.1  

Option 2: Function.prototype.bind

The above example with IIFE looks quite complex and mouthful. As we now have an idea on how to solve the problem on hand, all we need to do is force create new scopeand Function.prototype.bind can be used to do so.

JavaScript 

  1. 'use strict'    
  2.   
  3. for (var i = 0; i < 10; i++) {     
  4.    /* 
  5.          (<function(i)>).bind(this, i) forces a new scope to be created *copying* 
  6.          the current *value* of i 
  7.    */     
  8.     setTimeout((function (i) {   
  9.          console.log('"i" is equal to ' + i + ' (after ' + i + ' secs)');     
  10.          }).bind(this, i), (1000 * i));   
  11. }    
  12.   
  13. // Code 1.1.2  
This version is very much similar to Code 1.1, only difference is instead of setTimeout(<function>,timeout) we have setTimeout( (<function(i)>).bind(this, i), timeout). So instead of asking setTimeout to defer <function> we are asking it to defer a copy of <function>bound to current value of i. 

Option 3: The ES6 way!

Let is almost exactly same as var except the fact that it creates lexically scoped variables. Variables created declared with let have limited to the scope they are defined in, in case of for loop, for each iteration, new copy of i is created which limits to that particular iteration.

JavaScript 
  1. 'use strict'   
  2.   
  3. /*    We are using `let` instead of `var`, `let` creates a `lexically` scoped 
  4.       variable i, meaning, for each iteration of for loop a new copy of i is 
  5.       created, so closure captures the `lexically` scoped reference  
  6. */   
  7.       for (let i = 0; i < 10; i++) {  
  8.          setTimeout(function () {  
  9.            console.log('"i" is equal to ' + i + ' (after ' + i + ' secs)');     
  10.             }, (1000 * i));   
  11.       }   
  12.   
  13. // Code 1.1.3  
All of these three options produce following output.

JavaScript

  1. "i" is equal to 0 (after 0 secs)   
  2. "i" is equal to 1 (after 1 secs)   
  3. "i" is equal to 2 (after 2 secs)   
  4. "i" is equal to 3 (after 3 secs)   
  5. "i" is equal to 4 (after 4 secs)   
  6. "i" is equal to 5 (after 5 secs)   
  7. "i" is equal to 6 (after 6 secs)   
  8. "i" is equal to 7 (after 7 secs)   
  9. "i" is equal to 8 (after 8 secs)   
  10. "i" is equal to 9 (after 9 secs)    
  11.   
  12. // Output for 1.1.1, 1.1.2, 1.1.3  

A word of Caution

As you can see, closures are powerful, but as uncle ben once said With great power comes great responsibility. Unnecessary usage of closure can make your code inefficient. Because every time a closure is created memory is consumed to store and maintain the references, sometimes this will result in poor quality program. So always make sure that you eliminate any unnecessary and unintended closures. For example, one should not write functions inside functions and access parent functions values when there are better ways to do the same thing. Because doing so would create unnecessary closure. Take a look at following example

JavaScript 
  1. 'use strict'   
  2.   
  3. function Car(make, model) {      
  4.    return{  
  5.      setMake: function (value) {  
  6.           make = value;  
  7.      },  
  8.      getMake: function () {  
  9.        return this.make;  
  10.      },  
  11.      setModel: function (value) {  
  12.        model = value;  
  13.      },  
  14.      getModel: function () {  
  15.        return model;  
  16.      },  
  17.      print: function () {  
  18.        console.log(make, model);  
  19.      }  
  20.     }  
  21.  }  
  22.   var skodaSuperb = Car('Skoda''Superb');  
  23.   skodaSuperb.print();  
  24.   
  25.   skodaSuperb.setModel('Octavia');  
  26.   skodaSuperb.print();  
  27.   
  28.  // code 1.4  
output

JavaScript
 
  1. Skoda Superb   
  2. Skoda Octavia   
  3.   
  4. //output 1.4  
This code heavily uses closures, but does not need to, there are better ways to do it, for example look at following example.

JavaScript 
  1. 'use strict'   
  2.   
  3. function Car(make, model) {  
  4.      this.make = make;  
  5.      this.model = model;  
  6.      this.setMake = function (make) {  
  7.        this.make = make;  
  8.      }  
  9.      this.getMake = function () {  
  10.        return this.make;  
  11.      }  
  12.      this.setModel = function (model) {  
  13.        this.model = model;  
  14.      }  
  15.      this.getModel = function () {  
  16.        return this.model;  
  17.      }  
  18.      this.print = function () {  
  19.        console.log(this.make, this.model);  
  20.      }  
  21.  }  
  22.   
  23.   var skodaSuperb = new Car('Skoda''Superb');  
  24.   skodaSuperb.print();  
  25.   
  26.   skodaSuperb.setModel('Octavia');  
  27.   skodaSuperb.print();  
  28.   
  29.  // code 1.5  
output

JavaScript
  1. Skoda Superb   
  2. Skoda Octavia    
  3.   
  4. //output 1.5  

Above code is written in the right direction and also looks fine, but if you take another look, you’d see that each function this.setMake, this.getMake, this.setModel, this.getModel and this.print creates a closure, which is clearly not required or used anywhere in the program. So it would be wise to remove them by following any of the two options

Option 1: Setting the prototype

It is wise to put the functions in the prototype, as shown below

JavaScript

  1. 'use strict'   
  2.   
  3. function Car(make, model) {  
  4.      this.make = make;  
  5.      this.model = model;  
  6. }    
  7.   
  8. /* attach functions to the prototype */   
  9. Car.prototype.setMake = function (make) {  
  10.    this.make = make;   
  11. }   
  12. Car.prototype.getMake = function () {  
  13.    return this.make;  
  14. }    
  15. Car.prototype.setModel = function (model) {  
  16.    this.model = model;   
  17. }   
  18. Car.prototype.getModel = function () {  
  19.    return this.model;   
  20. }    
  21. Car.prototype.print = function () {  
  22.    console.log(this.make, this.model);   
  23. }    
  24.   
  25. var skodaSuperb = new Car('Skoda''Superb');   
  26. skodaSuperb.print();    
  27.   
  28. skodaSuperb.setModel('Octavia');   
  29. skodaSuperb.print();    
  30.   
  31. //code 1.5  
output

JavaScript

  1. Skoda Superb   
  2. Skoda Octavia    
  3.   
  4. //output 1.5  
Above code is what should be written ideally. No Unnecessary closure are created and all’s well.

Option 2: Function.prototype.call (A cleaner way)

This method is very similar to option 1, actually we are writing option 1 is a more readable way.

JavaScript

  1. 'use strict'   
  2.   
  3. function Car(make, model) {  
  4.       this.make = make;  
  5.       this.model = model;   
  6. }    
  7.   
  8. (function () {  
  9.    this.setMake = function (make) {  
  10.      this.make = make;  
  11.    }  
  12.    this.getMake = function () {  
  13.      return this.make;  
  14.    }  
  15.    this.setModel = function (model) {  
  16.      this.model = model;  
  17.    }  
  18.    this.getModel = function () {  
  19.      return this.model;  
  20.    }  
  21.    this.print = function () {  
  22.      console.log(this.make, this.model);  
  23.    }  
  24.  }).call(Car.prototype);   
  25. //Passing Car.prototype as "this"   
  26. //using Function.prototype.call, attaching required functions   
  27. //to Car's prototype     
  28. var skodaSuperb = new Car('Skoda''Superb');   
  29. skodaSuperb.print();    
  30.   
  31. skodaSuperb.setModel('Octavia');   
  32. skodaSuperb.print();    
  33.   
  34. //code 1.6  
output
  1. Skoda Superb   
  2. Skoda Octavia    
  3.   
  4. //output 1.6  

or

Option 2.1 (Non object Oriented way)

function.prototype.call can be used differently (not like option 2’s Object Oriented Way)

JavaScript

  1. 'use strict'   
  2.   
  3. function Car(make, model) {  
  4.    this.make = make;  
  5.    this.model = model;   
  6. }  
  7.  function setMake(make) {  
  8.    this.make = make;   
  9. }  
  10.  function getMake() {  
  11.    return this.make;  
  12.  }  
  13.  function setModel(model) {  
  14.    this.model = model;  
  15.  }  
  16.  function getModel() {  
  17.    return this.model;  
  18.  }  
  19.  function print() {  
  20.    console.log(this.make, this.model);  
  21.  }  
  22.   
  23.   var skodaSuperb = new Car('Skoda''Superb');  
  24.   //this is set to skodaSuperb for print function   
  25.   print.call(skodaSuperb);    
  26.   
  27.   //this is set to skodaSuperb for setModel function   
  28.   setModel.call(skodaSuperb, 'Octavia');   
  29.   print.call(skodaSuperb);    
  30.   
  31. // code 1.6.1  
output

JavaScript

  1. Skoda Superb   
  2. Skoda Octavia    
  3.   
  4. //output 1.6  
There are more ways to get the same thing done, and you’d be the judge of whats the best given the scenario.

What now?

This was my attempt to explain closure and how it affects almost every line of code that we write in javascript and how to take advantage of it and how to avoid it, how to create new execution scopes etc.

In coming articles we’ll take a look into functional programming with javascript, then on basic building blocks of functional programming - call, bind and apply along with very handy higher order functions like map, reduce and filter.

Hope you guys enjoyed this article. Feel free to comment below, See you soon.

Cheers and Happy coding !!

Read more articles on JavaScript:

Up Next
    Ebook Download
    View all
    Learn
    View all