Why does changing one array alters the other?

It’s the same array (since it’s an object, it’s the same reference), you need to create a copy to manipulate them separately using .slice() (which creates a new array with the elements at the first level copied over), like this:

var a = [1, 2, 3],
    b = a.slice();

b[1] = 3;

a; // a === [1, 2, 3]

Leave a Comment