Related Articles

Related Articles

What is the practical use for a closure in JavaScript?
  • Last Updated : 11 Jun, 2020

In JavaScript, closures are defined as inner functions that have access to variables and parameters of outer function even after the outer function has returned. The below examples show the practical use of closures:

Example: In this example, a variable mult is defined that is local to the function multFn and is only accessible inside this function. When an inner function is declared, JavaScript creates a closure where the inner function has access to the variable and its parameters.

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Define the closure
function multFn() {
  var mult = 9;
  return function(val) {
    mult = mult * val;
    return mult;
  }
}
  
// Use the closure
var mult = multFn();
console.log(mult(18));

chevron_right


Output:

162

1. Using private variables and methods: In JavaScript, we can use private variables and methods using closures. The example below shows the use of private variables with closure.



Example: In this example, the rentPrice() function returns an object with three methods: getRent(), incRent(), and decRent(). These three methods has access to the private variable rent. However, the code outside its scope cannot directly access this variable. Hence, we can mimic object oriented programming in JavaScript.

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Define the closure
var rentPrice = function(initialRent) {
   var rent = initialRent;
  
    // Defien private variables for
    // the closure
    return {
      getRent: function() {
        return console.log(rent);
      },
      incRent: function(amount) {
        rent += amount;
        console.log(rent);
      },
      decRent: function(amount) {
         rent -= amount;
         console.log(rent);
      }
    }
}
  
var Rent = rentPrice(8000);
  
// Access the private methods
Rent.incRent(2000);
Rent.decRent(1500);
Rent.decRent(1000); 
Rent.incRent(2000); 
Rent.getRent();

chevron_right


Output:

10000
8500
7500
9500
9500

2. Maintaining state between each function call: Suppose there is a function and one would like it to multiply multiple values together. This could be done with the help of a global variable as they are accessible throughout the program. However, a drawback is that they are prone to change from anywhere in the code. This can be done alternatively using closures. Closures help in maintaining the state between function calls without using a global variable.

Example:

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

(function() {
  
  var multFn = function multiply() {
    // This variable is local to
    // the closure and holds
    // its value inbetween
    // multiple calls
   var mult = 9;
   return function(val) {
     mult = mult * val;
     return mult;
   }
  };
  
  var mult = multFn();
    
  // Call the method
  // multiple times
  console.log(mult(2));
  console.log(mult(3));
  console.log(mult(5));
}());

chevron_right


Output:

18
54
270

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :