3 ways to clone in JS

Objects in JavaScript are reference values, you can’t simply just copy using the ‘=’

Save It For Later! Let’s begin…

const food = {a : ‘apple’, b : ‘burger’}

1. Spread

{…food}

2. Object.assign

Object.assi…


This content originally appeared on DEV Community and was authored by Rupak Dey

Objects in JavaScript are reference values, you can't simply just copy using the '='

Save It For Later! Let's begin...

const food = {a : 'apple', b : 'burger'}


1. Spread

{...food}


2. Object.assign

Object.assign({}, food)


3. JSON

JSON.parse(JSON.stringify(food))


Result

{a : 'apple', b : 'burger'}


Do you know the difference between these 3 ways? Comment down below!


This content originally appeared on DEV Community and was authored by Rupak Dey


Print Share Comment Cite Upload Translate Updates
APA

Rupak Dey | Sciencx (2021-05-31T12:42:22+00:00) 3 ways to clone in JS. Retrieved from https://www.scien.cx/2021/05/31/3-ways-to-clone-in-js/

MLA
" » 3 ways to clone in JS." Rupak Dey | Sciencx - Monday May 31, 2021, https://www.scien.cx/2021/05/31/3-ways-to-clone-in-js/
HARVARD
Rupak Dey | Sciencx Monday May 31, 2021 » 3 ways to clone in JS., viewed ,<https://www.scien.cx/2021/05/31/3-ways-to-clone-in-js/>
VANCOUVER
Rupak Dey | Sciencx - » 3 ways to clone in JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/31/3-ways-to-clone-in-js/
CHICAGO
" » 3 ways to clone in JS." Rupak Dey | Sciencx - Accessed . https://www.scien.cx/2021/05/31/3-ways-to-clone-in-js/
IEEE
" » 3 ways to clone in JS." Rupak Dey | Sciencx [Online]. Available: https://www.scien.cx/2021/05/31/3-ways-to-clone-in-js/. [Accessed: ]
rf:citation
» 3 ways to clone in JS | Rupak Dey | Sciencx | https://www.scien.cx/2021/05/31/3-ways-to-clone-in-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.