DEV Community

Atif Riaz
Atif Riaz

Posted on • Edited on

3 Ways to Clone Objects in JavaScript

Since Objects in JavaScript are reference values, you cant just Copy Using the =. But no worries, here are 3 different ways for you to Clone an Object

  1. Spread
  2. Object.assign
  3. JSON
const food = { beef: '🥩', bacon: '🥓' }

// "Spread"
{ ...food }

// "Object.assign"
Object.assign({}, food)

// "JSON"
JSON.parse(JSON.stringify(food))

// RESULT:
// { beef: '🥩', bacon: '🥓' }
Enter fullscreen mode Exit fullscreen mode

3 Ways to Clone Objects in javascript

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay