As I understand, object references x in x = { greet: hi } stores a reference to object { greet:hi} unlike primitive types that hold actual values ( y=10)
In the following code, console.log(y) outputs {greet: "hi"}.
Why does y object reference is not updated to point to {greet: 'hello'} when x obj ref is updated to point to x = {greet: 'hello'}
var x = {greet: 'hi'};
var y = x;
x = {greet: 'hello'};
console.log(y);
console.log(x)
y = xmakesyand alias ofx, i.e. that they're both the same variable really. That's not how it works. You're merely assigning the object reference stored inxtoy.x = {greet: 'hello'};, you are replacing the reference itself. So now the variable will hold pointer to some other location and object at previous location would remain as is. If no one is referencing it, GC will get rid of it.