Basic Data Structures: Copy an Array with the Spread Operator

Tell us what’s happening:

I have figured out a solution and found out where the mistake (syntax) lies. But I can’t figure out why the brackets are needed here. I’d like an explanation to understand the difference between.

Crucial part of my code that doesn’t work :

newArr = [...newArr,...arr];

But it is accepted like this :

newArr = [...newArr,[...arr]];

So … is a nested array. the first example puts both arrs in one giant array. The second one nests one array in the other.

So first one makes it like [1,2,3,1,2,3,1,2,3] whereas second one makes it like [[1,2,3],[1,2,3],[1,2,3]] ?

More like: [1,2,[1,2,3]]

When you do these challenges I find it helps to keep you console log open so you can really see the out put.

Oh I was having problems with console log I thought it would print it on the website but got it now, I have to open the browsers console to see the output. I’ve even tried using ideone.com but unable to make it work, it gave runtime error and etc. but from now on I know what to do, thank you very much :smile:

1 Like