Why are these two different behaviors?
when you assign an object to another variable, the new variable points to the same objects, therefore, when the properties of the new variables change, the objects become mutated Example:
var a= {name: "some"}; var b = a; b.name = "newName"; console.log(a.name);
when you assign a primitive type to another variable, its just a new variable and you don't refer to the old variable, so changing the new variable will not affect the old one. Example:
var a = "some"; var b = a; b = "newName"; console.log(a);
hope this helps!
source share