-1
    function times(x){
      return function(y){
        return function(z){
          return  x*y*z;
        }
      };
    }

A bit confused how does this exactly work? How does that function know that the next parentheses is the argument for the inside return function?

console.log(times(2)(5)(2))  // 20
Davis Davis
  • 429
  • 4
  • 12

5 Answers5

2

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.

David
  • 176,566
  • 33
  • 178
  • 245
0

When you write times(2)(5)(2), you are basically doing ((times(2))(5))(2), which means times(2) will be evaluated, which will return a function, then that function will be evaluated with (5) and so on.

A better example:

function a(){
    return {b: 'c'}
}

a().b //c
Rodrigo Leite
  • 568
  • 3
  • 19
0

It means that the first function returns another function which in turn returns another function.

It is same as calling all the three functions one after the other.

var func1 = times(2);
var func2 = func1(3);
var func3 = func2(4);
Venky
  • 3,878
  • 4
  • 36
  • 72
0

One after another:

function times(x){
  return function first(y){
    return function second(z){
      return  x*y*z;
    }
  };
}

var progress=times(1);

Progress is now a pointer to first, as first was returned by times. X is bound to it ( have a look at Closures...)

progress=progress(2);

Progress is now second, as Second is returned by First.

progress=progress(3);//1*2*3
Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
0

The concept is called currying.

So what it does is this :

var result1 = times(2); //==> returns a function
var result2 = result1(5); //==> returns a function
var result3 = result2(5); //==> returns a product
Jaya
  • 3,284
  • 2
  • 27
  • 46
  • The concept of returning a function from within a function is not currying, that is the concept of closures. Yes you *can* curry in this way, but true currying checks the number of arguments passed and determines the function (if any) to return. True currying in this example would return a product if you said `times(2,5,5)` – mhodges May 31 '17 at 17:38
  • why the f this has been downvoted when the duplicate marking is based on what I mentioned here – Jaya Jun 02 '17 at 19:41
  • I did not downvote this, but it looks like someone came by and downvoted all the answers. You are correct in that the question marked as a duplicate is about currying, however, it has been incorrectly marked, as I mentioned in my comment. However, they likely downvoted because answering duplicate questions is frowned upon. – mhodges Jun 02 '17 at 20:00
  • @mhodges I apologize upfront if i sounded accusing , i did not mean to ask you "why the downvote", in general I had asked why. And I do see your point in saying why this isnt exactly currying but could be called as part of it - All dogs are animals but not all animals are dogs analogy – Jaya Jun 02 '17 at 20:43