3 ways to clone in JS
Rupak Dey
Posted on May 31, 2021
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!
Connect with me : Github | Tutoring | Freelance Web Dev
π πͺ π
π©
Rupak Dey
Posted on May 31, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
javascript How to Handle Errors When Accessing Context Outside the Provider in React
September 21, 2024