This:
times(2)
Executes the function times
. But look at what that function returns:
function times(x){
return function(y){
//...
}
}
It returns a function. So that function, as the return value of times(2)
, can itself be executed:
times(2)(5)
That function also returns a function, which itself is also executed:
times(2)(5)(2)
Basically, in JavaScript a function is an object like any other. It can be passed as an argument, returned, set to a variable, or just executed inline without ever needing to be named or otherwise referenced.
So this:
times(2)(5)
Is roughly equivalent to this:
var func = times(2);
func(5);
But without the need for a variable to store that initial return value, it just executes it in-line as it's returned.