0
let user = {
    name: "John",
    age: 30,
    address: {
        country: "England",
        city: "Manchester"
    }
}

let newUser = user; // shallow copy

newUser = newUser.address; // I assigned the nested object to newUser variable. (what happened in this place? Did I create a new object in memory? but if I make this(newUser = {country:"testing", city: "xxx"}), it will create a new object in memory. What is the differences between them?)

newUser.city = "London" // I change city of newUser, but this affects User object. Why And How affect User object?

console.log("newUser", newUser);
console.log("user", user);  
Pointy
  • 405,095
  • 59
  • 585
  • 614
mert
  • 5
  • 4

0 Answers0