Getting stuck on some expressions on ES6 section

Tell us what’s happening:
Hey guys, i am on ES6 section and the stuff are getting more complicated and the course are so fast but it’s funny though.
up to now i am confused with functions inside each other, two returns one in another and these braces, brackets and last semi colon at the last line
could anyone help me get these stuffs
Thanks
first question: })();
second are in Rest Operator challenge:

const sum = (function() {
  "use strict";
  **this one ->** return function sum(...args) {   
    //const args = [ x, y, z ];
   **and this ->** return args.reduce((a, b) => a + b, 0); 
  };
})();
console.log(sum(1, 2, 3)); // 6

Your code so far


const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;
(function() {
  "use strict";
  arr2 = [...arr1]; // change this line
})();
console.log(arr2);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place

@MBrian161 I understand how you may be confused by this representation of the code. Actually, we have recently restructured all challenges which have this look, because it was confusing for many campers. The changes are not yet it production, but should be live in the month.

When you see the following pattern, this is known as an Immediately Invoked Function Expression (IIFE).

(function() {
  // do something
})();

It is a way of executing a specific section of code vs. declaring it as a function and then calling it.

The above is the equivalent as doing the following:

function executeSomeCode() {
  // do something
}
executeSomeCode();
1 Like

yeah, i read something about it’s wrapped in IIFE something like that.
so the expression if it calling itself simple works like recursive function.
Am i wrong??
and thank you for help

It has nothing to do with recursion. The only reason these were included with these challenges has to do with the way the back-end testing used to work for these ES6 challenges.

IIFEs are useful in creating modules.

See https://medium.com/@kelin2025/so-you-wanna-use-es6-modules-714f48b3a953

1 Like