I know this works, but I don’t know why or the reasoning behind why it was made to work this way:
var foo = [5, 10];
var bar = foo;
console.log(foo); //[5, 10]
console.log(bar); //[5, 10]
bar[0] = 1;
console.log(foo); //[1, 10]
bar = null;
console.log(foo); //[1, 10]
I would have expected not just bar to become null, but foo as well. I’d love some help understanding this.
The difference is between rebinding and mutating operations.
is mutating; it affects the object that
barpoints to.is rebinding; it just affects what the identifier
barmeans.