'Javascript objects equality vs using the spread operator to create new objects [duplicate]
When I have an object defined as such,
const obj = {
first: "hey",
second: "hello"
}
and I do not wish to modify this but create a new object and reassign values to that.
I though doing this should work:
const testObj = obj;
testObj[first] = "heyyy"
However, this causes the original 'obj' object to modify as well. I figured out that using the spread operator will be a solution for this, however, can someone explain why doing testObj = obj, is causing the 'obj' object to be modified every time testObj is modified.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
