Test 1:
var arr1 = [10, 20];
var arr2 = arr1;
arr2[0]++;
alert(arr1);// [11, 20] --- What?!?!
alert(arr2);// [11, 20] --- correct
Test 2:
var arr1 = [10, 20];
var arr2 = [arr1[0], arr1[1]];
arr2[0]++;
alert(arr1);// [10, 20] --- correct
alert(arr2);// [11, 20] --- correct
In test 1, Why the first array's first element was changed?
As far as I know, in other OOP languages like Java & PHP, if we do the Test 1's var arr2 = arr1; this is called "referencing", once we change something with the new variable's value, it creates a new value for that so the first reference "arr1" doesn't get effected. Why it's not the same thing in Javascript!? This totally doesn't make sense! Is it a bug? or it's just that Javascript behaves differently?