Use the Spread Operator to Evaluate Arrays In-Place--- already solved, dumb question

Tell us what’s happening:

I already solved this challenge, just wanna know why does the example code has that extra parentheses on line 6, thanks in advance

Your code so far


const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;
(function() {
  "use strict";
  arr2 = []; // 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/74.0.3729.131 Safari/537.36.

Link to the challenge:
wont let me post link aha sorry

1 Like

That’s definitely not a stupid question!

The function from lines 3-6 is known as an IIFE, or an Immediately Invoked Function Expression. Think of it as a shorthand for:

function doSomething() {
  console.log('did something')
}
doSomething()

Where you take all of the function doSomething and you literally replace the call on line six with that text, e.g.

function doSomething() {
  console.log('did something')
}()

Except that that second version won’t run. To make it runnable, you have to wrap the function itself in parenthesis, like so:

(function doSomething() {
  console.log('did something')
})()

I highly recommend this article by BenAlman on Immediately Invoked Function Experessions. It goes into a lot more detail than my simple example and really helped the concept “click” for me. :+1:

Hope that helps!

2 Likes

Mhmm its starting to make sense, will check that link, thanks a lot

1 Like