0 votes
in JavaScript by
Give an example of closure?

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

Related questions

0 votes
asked Oct 8, 2022 in JavaScript by rajeshsharma
0 votes
asked Dec 21, 2023 in C Plus Plus by GeorgeBell
...