re: Understanding the Spread Operator in JavaScript VIEW POST

VIEW FULL DISCUSSION
 

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

console.log(obj.b.a)
console.log(copy.b.a)

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