I defined a function let sum = a => b => b?(a+b):a;
If I do
console.log(sum(1)(2)());
I get the output 3
But If I try
console.log(sum(1)(2)(3)());
I get an error.
Why is that?
I defined a function let sum = a => b => b?(a+b):a;
If I do
console.log(sum(1)(2)());
I get the output 3
But If I try
console.log(sum(1)(2)(3)());
I get an error.
Why is that?
When you do
console.log(sum(1)(2)(3));
it will throw an error because you are trying to invoke the 3rd function; which you never created (sum(1)(2)
returns 3, which isn't a function).
Your function is a higher order function of two order. A function that returns another function. So the function call should be twice only; you mustn't call the 3rd function, as you did above.