Use the Rest Operator with Function Parameters//basic questions

Tell us what’s happening:

Your code so far

const sum = (function() {
  "use strict";
  return function sum(...args) {
   // const args = [ x, y, z ];
    return args.reduce((a, b) => a + b, 0); };
  
})();

console.log(sum(1, 2, 3, 4, 5));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters

I have some very basic level question about this.
I’m not sure what have I been missing out so far.

  1. // const args = [ x, y, z ]; why should this part be omitted?
    My guess is because …args create an array called args,
    an array called args shouldn’t already exist. is that right?
  2. I don’t understand what () at the very end of the code stand for.
const sum = (function() {

} ) ();

so i have a question about what these curly braces
that wraps around the whole code block stands for,
and why is it inside of const sum = (function() {codes} ) and what is this (); at the end.
I feel like I missed some important lessons about structures of codes…
what lesson should i review ?
Thanks.

1 Like

These are the lessons related to what you’re asking for:
Freecodecamp [OOP] - IIFE
Freecodecamp [OOP] - Closures

And here few useful resources ( mdn references):
MDN - IIFE
MDN - Closures

That said those pattern are not required by the actual challenge, a lot of people are confused by that code :slight_smile:

1 Like

Thank you so much !! I’ve learned a lot :slight_smile:

1 Like