DEV Community

Understanding the Spread Operator in JavaScript

Marina Mosti on February 26, 2019

{...object} The first time I looked at this I was really confused about what it was supposed to mean, or even do, but harnessing the power of the ...
Collapse
 
sleepyfran profile image
Fran González • Edited

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.

Collapse
 
marinamosti profile image
Marina Mosti

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

Collapse
 
qm3ster profile image
Mihail Malo • Edited
const randoms = function*(x){while(true)yield Math.random()*x|0}
const [a, b, c] = randoms (1000)
console.log(a, b, c)

open eye crying laughing emoji

Collapse
 
jxnday profile image
Jackson Day • Edited

Great intro to spread! Looks super useful for making copies. Though at the end, I believe you mean to say

original.child == copy.child

(not

original.child == original.copy

) is that right?

Collapse
 
marinamosti profile image
Marina Mosti

🤦‍♀️This is why you dont hotfix posts while doing 3 other things. Thanks! :)

Collapse
 
rher profile image
Raul Hernandez

Hi Marina,
thanks a lot for your instructive and funny to read article. Like your style.

I would like to ask you (if you ever revisit this article) , about what you say

both obj and anotherObj are actually pointing to the same object. So if you change one or the other, they will both change because the pesky pointer

How the object should be changed in order to change also its copy?
I just tried in the console to do

baseObject.name="otherName
And when I consolelog both, the copy remain untouched.

Thanks in advance if you ever take the time to answer!

Collapse
 
devdammak profile image
Damola Adekoya

I love spread operator it has saved me lot of times.. instead of using Array.prototype.slice..
Because I always mix it up with Array.prototype.splice...

Collapse
 
marinamosti profile image
Marina Mosti

All ways lead to Rome :) But this one is definitely one of the easier/shorter ones

Collapse
 
devdammak profile image
Damola Adekoya

Yeah

Collapse
 
csabdulwahab profile image
Abdul Wahab

Once again thankyou

I have read so many articles on the same topics
But you are special. I know i should be writing the code as well at the same time
But here i go looking to read another article from you

Collapse
 
ipas profile image
iPAS

Does it seems like in Python? that

func(1, 2, a=1, b=2)

def func(args, **kwargs):
print(*args) --> [1, 2]
print(
*kwargs) --> {a: 1, b: 2}

Collapse
 
drozerah profile image
Drozerah

Really nice and easy way to make copy ! :)

Collapse
 
marinamosti profile image
Marina Mosti

Thanks Drozerah!

Collapse
 
eidsonator profile image
Todd Eidson

I always preferred the term 'splat' operator. It sounds so much cooler. Nice article, thanks!

Collapse
 
marinamosti profile image
Marina Mosti

Thanks for reading :)

Collapse
 
wtaylor45 profile image
Will Taylor

Really great explanation of this! Especially like that you gave the example of copying an object in that way. That's a super important use case.

Collapse
 
marinamosti profile image
Marina Mosti • Edited

You're right, it's very simple and super useful :)

Collapse
 
birju112 profile image
Birju Shah

Nice article. Would you mind mentioning that the copy with the spread operator creates a shallow copy of the object and not a deep one.

Collapse
 
marinamosti profile image
Marina Mosti

Hey Birju, you're right. I try to keep these as simple as possible and sometimes overlook some of the more advanced concepts in favor of clarity. Will make a note, thanks

Collapse
 
grandemayta profile image
Gabriel Mayta • Edited

Great article, very clear!

Collapse
 
marinamosti profile image
Marina Mosti

Hey Gabriel, thanks a ton :D