Please me to understand the closures. Why does the counter work in the first variant, but in the second version there is not?
var counter = (function(){
var count=0;
return function(){
return count++;
}
}());
console.log(counter());
console.log(counter());
console.log(counter());
The counter outputs 0,1,2
var counter = function(){
var count=0;
return function(){
return count++;
}
};
console.log(counter()());
console.log(counter()());
console.log(counter()());
The counter outputs 0,0,0
What is the difference?