Rest Operator with Function Parameters what are () for

Tell us what’s happening:
What are the empty set of parenthesis for? Right before:
console.log(sum(1, 2, 3)); // 6

I put different values in it and it does not seem to be doing anything, but if I remove them the console.log prints out:
[Function: sum]

I don’t get it
Your code so far


const sum = (function() {
  "use strict";
  return function sum(...args) {
    
    return args.reduce((a, b) => a + b, 0);
  };
})();
console.log(sum(1, 2, 3)); // 6
1 Like

Good question, I’m not sure.
But removing the () returns the function as it is (as if it were a string).

so maybe it’s like this
const sum = something();
but instead of something we have the declaration of the whole function including the () at the end.

() is the syntax that tells JS to execute a function. add(1,2), or printSomething("foo"). The bit you’re talking about, the bit that wraps the code you’re asked to alter, that’s just for the benefit of the tests.

It may make more sense if I write it like this (this is not quite how it is working in practice, but it is easier to understand the syntax):

const sum = function() {
  "use strict";
  return function sum(...args) {
    
    return args.reduce((a, b) => a + b, 0);
  };
});

sum();

What it is a function that immediately executes, they are used in JS to make a little environment inside which other code can live.

1 Like

I’m still not sure what you mean, cause the code you wrote gives an error.
Is it close to what I explained above ? that you add the FUNCTION() where FUNCTION is being replace by the its actual code ?

The () parenthesis are used to execute the function, so you could have

const func = function(...) {...};

And in this case func holds the function and if you write func(..) you call the function
Or you could have

const func = (function(...) {...})(...);

And in this case func is what the function returns as the function is called in place.

In the case of this challenge, the immediately called function is used as a way to create a closed environment to activate strict mode, and it returns the function inside it, so you can ignore it and don’t touch it and change the code inside the function where it is written that you can and it will work as normal with functions.

2 Likes

I think I just get confused because there are so many parenthesis at different levels, I thought that the parentheses that execute the “sum” function on my example were already there, opening right before the word “function” and closing right after the last curly brace, but then there is an extra set empty which makes me confused…

const sum = (function() {
“use strict”;
return function sum(…args) {

return args.reduce((a, b) => a + b, 0);

};
})();
console.log(sum(1, 2, 3)); // 6

Thank you guys.

btw this is another exaplanation on StackOverflow

Those are not the parenthesis they call the function, they surround the function body instead. The parenthesis that call the function are after the function body or after the variable get holds the function and are or empty or containing function arguments

If you want to read more about it, search for “IIFE”

1 Like

Those are put there before the function keyword (wrapping around the function) to trick the JS compiler into treating it differently, so I can understand where the confusion come from: after you’ve used JS for a while it just seems normal, but it looks alien otherwise.

1 Like

thanks everyone for your help, I am new to JS and the ES6 exercises on freecodecamp seem to assume one is already very familiar with JS but in reality I barely made it through understanding the “Basic Javascript” section. Can I anyone recommend any other resources for JS where it is easier to understand for someone who has never taken programming or CS classes before? basically like a JS for dummies.

Thanks I will check it out, I feel like many of the topics I am encountering in the ES6 section were never introduced on the Basic JS section,I am not sure if IIFE were presented in the basic js section but I see how one would have to know that prior to making sense to the example I posted about.