'Value overwriting in JavaScript [duplicate]

I created an object const with a data and I made other const from the first. But my problem is when I change the second object, my first const also is changed.

Have a method to resolve this?

const user1 = {
  name: "samuel"
}

const user2 = user1;

user2.name = "guedes";

console.log(user1); //output: "guedes"
console.log(user2); //output: "guedes"


Solution 1:[1]

Yes, you can use es6

const user1 = {
  name: "samuel"
}

const user2 = {...user1};

user2.name = "guedes";

console.log(user1); //output: "guedes"
console.log(user2); //output: "guedes"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arezou Saremian