DEV Community

Discussion on: Passed By Reference Vs. Value In Javascript

Collapse
 
bluma profile image
Roman Diviš

Don't forget about differences between shallow and deep copies...

const obj = { first: { second: 1 } }

const copy = Object.assign({}, obj)
copy.first.second = 2

console.log(obj.first.second) // -> 2

const copy2 = { ...obj }
copy2.first.second = 3

console.log(obj.first.second) // -> 3
Enter fullscreen mode Exit fullscreen mode

Both mentioned methods create only shallow copy of object.

Collapse
 
bbarbour profile image
Brian Barbour

Very true! I didn't want to get to deep into that as it may add confusion, but it's a good thing to look into next for sure!