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
Top comments (2)
One difference is that JSON.stringify will break if your object has circular references - you can use a custom replacer to prevent this stackoverflow.com/questions/116166...
Sure! Will check those out. Thank you