0

First of all, I am sorry for not understanding by reading it and I am a beginner in javascript. I am learning better with examples which I am familiar with.

function multiplier(factor) {
return number => number * factor;
}
let twice = multiplier(2);
console.log(twice(5));
// → 10

I was reading Eloquent_Javascript book and I encountered with this code. I basically understand the intention of the code and the result. I don't understand why does this give this result. Shouldn't be twice equal to 4 since multiplier should multiply 2 at first place then the result 20? For a split second, I kind of understand but still confused.

Haydar Öztürk
  • 122
  • 1
  • 13
  • What you're doing is `5 * 2 = 10` – Get Off My Lawn Jun 29 '18 at 18:35
  • 2
    The expression `number => number * factor;` **creates a function**. It (the expression itself) doesn't do any multiplication. (The created function does when it is called). – Quentin Jun 29 '18 at 18:36
  • @getoffmylawn I understand that part but why I do it? Does number in multiplier function only returns the factor when number function hasn't been defined? If so, how come it doesn't ignores the number second time? – Haydar Öztürk Jun 29 '18 at 18:37
  • There is no "number" function. The two functions you have are `multiplier` and `twice` – Quentin Jun 29 '18 at 18:38
  • @Quentin Thank your for your answer. But why doesn't do it at first time but does it in second time? – Haydar Öztürk Jun 29 '18 at 18:38
  • What second time? – Quentin Jun 29 '18 at 18:39
  • @Quentin I mean, when Multiplier(2) is there, shouldn't it be returned as 4 than twice 5 called it would be 20 if 4 is still there or 25 if only 5 exists? I am sorry maybe I am too dumb but I really don't get it. – Haydar Öztürk Jun 29 '18 at 18:41
  • No. It should not be `4`. It should be the function that `multiplier` returns. You put `2` in, you get a function out. There is nothing that would multiply `2` by itself. – Quentin Jun 29 '18 at 20:14

0 Answers0