Consider:
let myArray = [1, 2, 3];
let myArray2 = myArray;
let myArray3 = [...myArray];
What is the difference between the assignments for myArray2
and myArray3
? Is myArray2 a reference to the same memory as myArray (so if myArray.shift()
, the value of myArray2
also changes), whereas myArray3
is a copy of myArray
?
Clarification: I understand [...myArray]
creates a copy of the array. I'm not clear if myArray2 = myArray
also creates a copy, or creates a a second reference to the same array, so changing the array via one will be reflected when getting the value via the other.