0
var t1 = [2,4,7,9];
var t2 = t1;
console.warn(t2); // [2, 4, 7, 9]
t1[1] = 7;
console.warn(t2); // [2, 7, 7, 9]

Why? :) I can't understand. t2 is inheits t1?

  • Arrays are passed as "reference of a value", see the duplicate for a much longer explanation, and that's why you're working with the same array in both variables, and not unique arrays. – adeneo Oct 15 '15 at 17:57
  • 1
    To solve the issue for you, if you want to copy the array, you can do `var t2 = t1.slice();` – adeneo Oct 15 '15 at 17:58

0 Answers0