Suppose you have the following code:
var array = [1];
var array2 = array;
array2.push(2);
alert(array);
This will alert "1,2", even though I clearly declared array
as [1]
, and then did no changes to it. It's like array
and array2
gets connected, and every change I do to array2
affects array
. I simply want to declare array2
with the same value as array
, not connect them.
Why does this happen and how to prevent it?
Thanks in advance!