- What is difference / similarities between calling a function like the following ?
- What is the purpose of having these two ways?
- What are their pros and cons?
- Can someone explain how this call works ->
sum(2)(3);
? And mention other conventional equivalent of this call ?
The sum function for both this call can be created like the following code
function sum(x) {
if (arguments.length == 2) {
return arguments[0] + arguments[1];
} else {
return function(y) { return x + y; };
}
}
console.log(sum(2,3)); // Outputs 5
console.log(sum(2)(3)); // Outputs 5