Javascript the reference value is not changing

Could you please help me understand why the value of ob2 is NOT ‘{}’

var ob1 = { name: ‘alex’};
var ob2 = ob1;
ob1 = {} ;

print ob2 and it print as { name: ‘alex’};

Because you initialise ob2 with the value of ob1 BEFORE you declare ob1 to be {}

ob1 has changed reference to an empty object

what you need to do is to change what’s inside the object, like delete ob1.name to have both variables affected

1 Like