0

When there are two arrays arr1, arr2 and assigning one array to another making one array null doesn't affect other? Any Specific reason??Array assignment in JavaScript. consider the below example..

var arr1 = [1,2,3,4,5,6,7,8,9,0];
        var arr2 = arr1;
        arr1[2]=10;
        console.log(arr1);//[1, 2, 10, 4, 5, 6, 7, 8, 9, 0] 
        console.log(arr2);//[1, 2, 10, 4, 5, 6, 7, 8, 9, 0]
        arr1 = [];
        console.log(arr1);//[]
        console.log(arr2);//[1, 2, 10, 4, 5, 6, 7, 8, 9, 0]
swa49
  • 17
  • 2
  • 3
    `arr2` stores a reference to a created array, as well as `arr1`, this reference is _a value_. Then you set `arr1` to an empty array, but `arr2` still refers to the original array. Objects are created somewhere in the memory, they are not "inside" any variable, variables only hold a reference to the objects. – Teemu Apr 15 '19 at 16:11
  • 1
    Possible duplicate of [How do you clone an Array of Objects in Javascript?](https://stackoverflow.com/questions/597588/how-do-you-clone-an-array-of-objects-in-javascript) – Mosè Raguzzini Apr 15 '19 at 16:16

0 Answers0