O
O
olya_0972018-01-31 19:01:57
JavaScript
olya_097, 2018-01-31 19:01:57

Write a sum function that works like this: sum(a)(b) = a+b, how to understand 2 returns?

Hello everyone, how to understand two returns? I understand that this is a return value, but in more detail? and 2 returns do closures? thanks
Write a sum function that works like this: sum(a)(b) = a+b
function sum(a ) {
return function(b) {
return a + b;
};
}
alert( sum(1)(2) ); //3
alert( sum(5)(-1) ); //4

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Litvinenko, 2018-01-31
@edli007

This means that the sum function will return another function

function sum() {
  return function() {
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question