DEV Community

Discussion on: Spread Operator: How Spread Works in JavaScript

Collapse
 
nombrekeff profile image
Keff

Nice article! Useful examples

I wanted to add something else, as I don't see many people aknowledging it. You say:

Alterations to myName will not reflect in aboutMe because the spread operator simply copied and paste myName’s content into aboutMe without creating any reference back to the original array.

This is true as long as the items inside the arrays are non-objects. As it only does shallow copy.

If you have this example:

const myName = [{ name: 'Giovanni' }];
const aboutMe = [{ description: 'I can do backflip' }];

const me = [...myName, ...aboutMe];
Enter fullscreen mode Exit fullscreen mode

In this case, if you modify an object in the me array, it will reflect in the original arrays, as they are just references.

I like to advice on this, as I've seen it cause some big issues.

Collapse
 
oluwatobiss profile image
Oluwatobi Sofela

Thanks, Keff, for highlighting this significant omission. I have now updated the article to reflect how spread works on objects with non-primitive values.

Collapse
 
nombrekeff profile image
Keff

No problem, I'm becoming the "spread-shallow-copy" guy lol!