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:
constmyName=[{name:'Giovanni'}];constaboutMe=[{description:'I can do backflip'}];constme=[...myName,...aboutMe];
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.
Thanks, Keff, for highlighting this significant omission. I have now updated the article to reflect how spread works on objects with non-primitive values.
Nice article! Useful examples
I wanted to add something else, as I don't see many people aknowledging it. You say:
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:
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.
Thanks, Keff, for highlighting this significant omission. I have now updated the article to reflect how spread works on objects with non-primitive values.
No problem, I'm becoming the "spread-shallow-copy" guy lol!