what is diffrent this 2 line maneing
this line copy all to arr
let […newarr] = arr
this line also same work
let newarr = […arr]
what is diffrent this 2 line maneing
this line copy all to arr
let […newarr] = arr
this line also same work
let newarr = […arr]
Hello!
There’s no difference . Could you explain what confuses you? Also, could you share the link to the challenge or where did you read it?
Edit
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 , nothing else to explain:
// This:
let [...newArray] = arr;
// is the same as:
let newArray = [...arr];
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 ]