Spread syntax confusing

what is diffrent this 2 line maneing

this line copy all to arr
let […newarr] = arr

this line also same work
let newarr = […arr]


There’s no difference :stuck_out_tongue:. Could you explain what confuses you? Also, could you share the link to the challenge or where did you read it?


Even though there is no difference in the result, if you were to define two variables with the same name using let, an error will be thrown.

i know
this code what different between behavior
can you explain me

It’s the same :stuck_out_tongue:, nothing else to explain:

// This:
let [...newArray] = arr;

// is the same as:
let newArray = [...arr];
1 Like

One is using rest the other is using spread.

// rest
let [...newArray1] = arr;
// spread
let newArray2 = [...arr];

In your example there isn’t really a practical difference but I’d suggest sticking with spread for doing array copying as that is the most common way of doing it and using rest might be misinterpreted as a bug with missing variables before the rest parameter.

const arr = [1, 2, 3, 4];

let [one, ...restOfArray] = arr;

console.log(one); // 1
console.log(restOfArray); // [ 2, 3, 4 ]

let zeroAddedToArray = [0, ...arr];

console.log(zeroAddedToArray); // [ 0, 1, 2, 3, 4 ]
1 Like