Understanding Closure in JavaScript in Javascript


Closures allow a function to access variables from an enclosing scope even after it leaves the scope in which it was declared.

Source Code

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