in JavaScript by
Q:
Give an example of closure?

► Click here to show 1 Answer

0 votes
by
Following example shows how the variable counter is visible within the create, increment, and print functions, but not outside of them −

function create() {

   var counter = 0;

   return {

      increment: function() {

         counter++;

      },

  

      print: function() {

         console.log(counter);

      }

   }

}

var c = create();

c.increment();

c.print();     // ==> 1
JavaScript
Learn More with Madanswer
...