Is exercise about spread operator correct?

I know that spread operator is used to unpack our array so we can do lots of things effortlessly. But look at this

const MY_ARRAY = [1,4,6,7,9];
console.log(...MY_ARRAY);
which gives me 1 4 6 7 9 in "console".

freeCodeCamp exercise : One key difference between the spread operator and array destructuring is that the spread operator unpacks all contents of an array into a comma-separated list.

Then why cannot I get a comma separated list? I am only see space separated list.

Great question. So try doing this: console.log("My", "name", "is", "Snowmonkey"). Tell me if it inserts the commas.

The spread operator is spreading the array elements, with commas, making them arguments to the console.log function. But how the console.log uses those comma-separated entries… is not what you think.

yeah, makes sense !

const MY_ARRAY = [1,4,6,7,9];
const newArray = [...My_ARRAY];
console.log(newArray.length) //returns 5

This would be only possible because of spread operator is actually giving a comma separated list. Else I would have got length of newArray to be 1.
I guess it’s okay to become little fool while learning . Thanks

1 Like

Not foolish at all, you asked a great question and then were able to think through and extend the answer. Nicely done.

2 Likes

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