Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptWhat is the practical use for a closure in JavaScript?

What is the practical use for a closure in JavaScript?

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




// 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));


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




// Define the closure
var rentPrice = function(initialRent) {
   var rent = initialRent;
  
    // Define 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();


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




(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));
}());


Output:

18
54
270

RELATED ARTICLES

Most Popular

Recent Comments