Simple Counter with Closure in Javascript


Use closure to create a private counter.

Source Code

function makeCounter() {
  let count = 0;
  return function() {
    return ++count;
  };
}
let counter = makeCounter();
console.log(counter()); // 1
console.log(counter()); // 2
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments