Probably this is a dumb question :)
var x = [1, 2, 3];
var y = x; // Value of x is stored in y
y[0] = 5;
alert(x[0]); // returns 5. Why?
x[0] ideally should be 1, but why is it 5?
Probably this is a dumb question :)
var x = [1, 2, 3];
var y = x; // Value of x is stored in y
y[0] = 5;
alert(x[0]); // returns 5. Why?
x[0] ideally should be 1, but why is it 5?
x contains address of first element of array and x = y passes that to y. So y[0] is same as x[0]. This is because x[0] yields address as x+0 and y[0] as y+0. Since y and x are same so y[0] and x[0] point to same location.