re: Understanding the Spread Operator in JavaScript VIEW POST


Good article! However, I feel like it's important to mention that the spread operator does not perform a deep clone on objects, but rather a shallow copy. This means that the spread operator will copy simple values (such as integers, strings and such) but keep the same references for objects. For example:

const obj = { a: 1, b: { a: 1 } };
const copy = {...obj}
copy.b.a = 2


This prints 2 and 2 because the property b was shallow copied, which makes copy.b point to the exact same object as obj.b.


Hey Fran, thanks for the addendum. I actually should have probably put a little more clarity on this part, but i've made a small correction and your example is also super clear

code of conduct - report abuse