I am currently dealing with understanding how values are assigned and passed around in Javascript. Primitive type's values are passed by value-copy, and object value types are passed by reference-copy.
If that is the case then why does this line of code not cause "yourAddress" variable to change the property of "street" to "321 cba street"?
var myAddress = {
street: "123 abc st",
};
var yourAddress = myAddress;
myAddress = { street: "321 cba street" }; // doesn't change the val
myAddress.street = "321 cba street"; // doesn't change the val
console.log(yourAddress.street);
Why doesn't re-assigning the variable, or modifying the property name affect also the output of yourAddress.street since it has the same reference as myAddress.
Is the reason for this hoisting or am I not understanding something here internally with JS?
The version of this code that would work is this but why?
var myAddress = {
street: "123 abc st",
};
myAddress = { street: "321 cba street" }; // doesn't change the val
myAddress.street = "321 cba street"; // doesn't change the val
var yourAddress = myAddress;
console.log(yourAddress.street);