About referencing objects and arrays

var arr = [1,2,3,4,5];
arr2 = arr;
arr = arr2.concat([6,7,8,9,10]);
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr2;
[1, 2, 3, 4, 5]

as in the code above, the array is a reference type object, and arr2 holds a reference to arr.
but why after operating on arr, the output arr2 is still the old value before arr.

Mar.03,2021
The
concat function returns a new reference


because concat does not change the original array


var arr = [1,2,3,4,5];
arr2 = arr; // arr2 = [1,2,3,4,5]
arr = arr2.concat([6,7,8,9,10]);
//concatarr
// arr2 [1,2,3,4,5],arr[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

as described upstairs;

Array.concat () returns a new array, so arr is no longer the same as the reference to arr2.

the details are

var arr = [1,2,3,4,5];   //heap_1[1,2,3,4,5] arr
arr2 = arr;  //arrarr2arr  arr2 heap_1

arr = arr2.concat([6,7,8,9,10]);  
//Array.concat() heap_2  arr heap_2;
//Array.concat()arr2heap_1
Menu