I have this code in JavaScript what is the difference between the lines A and B ?
const arr1 = [1,2,3,4,5]
const arr2 = [...arr1]; // Line A
const arr2 = arr1; // Line B
I want to know is it the same or there's some difference between the two assignments
They are different.
LINE A, copies (shallow copy) each element of the array to
arr2.LINE B, assigns a reference of
arr1toarr2. Basically,arr1andarr2are the same array.Example
Shallow Copy
Shallow copy only copies first-level items. For example, if the array contains another array, the inner array is copied, but not the elements of the inner array. So deeper elements are not copied. Please see the code example below: