0

I was following a beginners course of JavaScript and I've come across this code. But I don't understand why sum needs to be an IIFE function. Could you please help me understand how this code works exactly?

const sum = function() {
  return function sum(x, y, z) {
    const args = [x, y, z];
    return args.reduce((a, b) => a + b, 0);
  };
}();
console.log(sum(1, 2, 3))
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Jay02
  • 3
  • 2
  • this is just an example, you would also have `const foo = function() {....` and `console.log(foo(1, 2, 3))` – Mister Jojo Oct 13 '21 at 22:06
  • 2
    At least i don't see any reason in the code provided. Maybe there will later be code added, which requires it, but as-is, it doesn't need the iife. – ASDFGerte Oct 13 '21 at 22:07
  • It doesn't need to be an IIFE. It would work the same as `function sum(x, y, z) { const args = [x, y, z]; return args.reduce((a,b) => a + b, 0); }`. – Heretic Monkey Oct 13 '21 at 22:07
  • There doesn't seem to be a reason why this needs to be immediately invoked. If there was a variable in the closure that was keeping track of something it would make sense. But there isn't so might as well just declare it as a regular function and remove the IIFE wrapper – skellertor Oct 13 '21 at 22:07
  • 1
    As far as how the rest of the code works, when you run it through a debugger, what part don't you understand? – Heretic Monkey Oct 13 '21 at 22:09
  • Does this answer your question? [Will const and let make the IIFE pattern unnecessary?](https://stackoverflow.com/questions/33534485/will-const-and-let-make-the-iife-pattern-unnecessary) – Mark Schultheiss Oct 13 '21 at 22:10

3 Answers3

0

In this case the variable args takes the arguments

const sum = function() { 
  return function sum(x,y,z) {
     const args = [1,2,3];
     return args.reduce((a,b) => a+b, 0);}; 
}();
 console.log(sum(1,2,3))

The reduce() method executes a reducer function for each value of an array. In this case you are taking first two elements of arrays, finding the sum of these two and the reduce method goes one by one to other elements.

Other example to explain you better

const numbers = [175, 50, 25];

function myFunc(total, num) {
   return total - num;
} // returns 100 (175 - 50 - 25)
maca
  • 480
  • 3
  • 12
0

In that function looks like is just to show you how to work the IIFE(the parentheses at the end of the function) that parenthesis is to auto-execute the function, the use is if you want to execute a function when this load you can add the "()" at the end or "+" at the beginning of the clause "function" like this "+function" to see more in details check the following link:

Immediately-Invoked Function Expression (IIFE)

Jose Lora
  • 1,392
  • 4
  • 12
  • 18
0

There is no current reason the function is inside another executed function.

These are equivalent except that one gets returned immediately by a function. If I had to guess, the next instruction may talk about scoping or currying?, who knows.

const sum = function() {
  return function sum(x, y, z) {
    const args = [x, y, z];
    return args.reduce((a, b) => a + b, 0);
  };
}();
console.log(sum(1, 2, 3))

const sum = function(x, y, z) {
    const args = [x, y, z];
    return args.reduce((a, b) => a + b, 0);
  };
console.log(sum(1, 2, 3))
Namaskar
  • 2,114
  • 1
  • 15
  • 29