I’m getting different values for i by calling add function differently. Could someone explain what’s happening here?
var i = 0;
var add = function() {
++i;
return function() {
i++;
return function() {
i++;
add();
}
}
};
add(); // i = 1;
add()(); // i = 2;
add()()(); // i = 4;
The function
addreturns a function. And this function returns another function.So :
Each function just adds 1 to the
ivalue. That explains theivalue.