Help me to understand. Why when changing the value of the i-th element of array 2, the value of the i-th element of array 1 also changes, following the pattern of which array 2 was created. I cannot enter.

var array1 = [1, 2, 3]; var array2 = array1; array2[0] = 100; alert(array2) // [100, 2, 3] alert(array1) // [100, 2, 3] ????why???? 

  • array2.push (array1) - Mr. Black
  • you copied not an array, but a link to it. - Jean-Claude
  • All objects in JS are transferred and assigned by reference. And yes, the array is an object - Dmitriy Simushev
  • Thank you all, I will try to figure it out - pifagor2

1 answer 1

Because array2 is a reference to array1. This is essentially the same array, not a copy.