Hi, I have been recently trying to learn Javascript, and I came accross this code on w3schools website and I’m having a really hard time understanding it.
Here is the code:
var add = (function () {
var counter = 0;
return function () {counter += 1; return counter}
})();
add();
add();
add();
as far as i can understand, the code uses a nested function and it’s an anonymous self invoking function but i don’t understand how, when we execute it, the first time we get 1 aas output, then 2 , then 3 and so on. why is that?