DEV Community

Atif Riaz
Atif Riaz

Posted on • Updated 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)