What actually IS the spread operator?

Tell us what’s happening:

I have passed the test, but I don’t understand why. What exactly is the spread operator?? This definition is given:

ES6 introduces the spread operator, which allows us to expand arrays and other expressions in places where multiple parameters or elements are expected.

But doesn’t show an actual example of it being used?? apply( ) and Math.max( ) are mentioned and used, but neither are defined. I’m not opposed to doing further work and research to understand something, I’ve had to do it multiple times going through these courses. It’s just so unhelpful when new information is included and not explained when you’re being taught/trying to learn. :frowning_face:

I looked up apply( ) and Math.max( ) on w3schools and understand them. But I’m still left wondering: how do you actually use the spread operator? What does it look like?

Your code so far


const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;

arr2 = [];  // Change this line

console.log(arr2);

Your browser information:

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

Challenge: Use the Spread Operator to Evaluate Arrays In-Place

Link to the challenge:

The spread operator spreads out an array.
For example, the below line will output “[1, 2, 3, 4, 5]”, which is an array.

let arr = [1, 2, 3, 4, 5]
console.log([1, 2, 3, 4, 5])

Now with the spread operator below, it will output “1 2 3 4 5”. It displays the values in the array spread out.

let arr= [1, 2, 3 ,4 ,5]
console.log(...arr)

In the example, they use Math.max, as that will not accept an array, but accepts comma separated numbers. So if you put an array into Math.max, it would return NaN, as an array is not a number, but if you put Math.max(…arr) it spreads out the numbers and is no longer read as an array, but as the individual values within the array.

1 Like

Oh my goodness, thank you so much! That makes perfect sense, I understand it now! It didn’t make a lick of sense in the lesson, you broke it down in a way my brain accepts. Thank you so much for your help!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.