1

I've seen a code like this:

function operationType(type){
    return function(a,b){
        console.log(`It's a ${type}`);
        return a+b;
    }
}
const sum=operationType(type); 

has only one parameter called type? It looks like when i call the sum function it's only consider the returned function

And what i don't understand is they way arguments are passed when the function is called like this sum(4,4) and returns :

It's and addition
8

How can we call the function sum(4,4) when the operationType(type) function is function that's is really defined.

What I really want to say is here : const sum=operationType('addition') we do not pass the anonymous function that calculate the sum,how does it guesses??

How can sum(4,4) = operationType('addition').I'm really lost

Barmar
  • 669,327
  • 51
  • 454
  • 560
Christian Lisangola
  • 2,564
  • 3
  • 19
  • 31
  • This question is likely to be closed, so please read: https://developer.mozilla.org/en-US/docs/Glossary/First-class_Function, then see if you can whittle the question to something less broad that can be answered specifically. – Joe Frambach Jan 04 '20 at 01:12
  • `operationType` returns *a function*. So `const sum=operationType(type);` is essentially equal to `const sum = function(a,b){ console.log(\`It's a ${type}\`); return a+b; }` (the inner function that is returned from `operationType` – VLAZ Jan 04 '20 at 01:14
  • Also relevant question: [Two sets of parentheses after function call](https://stackoverflow.com/questions/18234491/two-sets-of-parentheses-after-function-call) – VLAZ Jan 04 '20 at 01:15

0 Answers0