DEV Community

 Bishnu Prasad Chowdhury
Bishnu Prasad Chowdhury

Posted on • Edited on

Copying Javascript objects

There are three types of syntax using which we can copy objects in javascript.

const area = {
  let length = 5,
  let breadth = 5,
  let type = 'square',
};
Enter fullscreen mode Exit fullscreen mode

//Shallow copy

  1. Using spread operator:
let sqr= {
  ...area
};
Enter fullscreen mode Exit fullscreen mode

//Shallow copy

  1. Using object method:
let sqr = Object.assign({}, area);
Enter fullscreen mode Exit fullscreen mode

//Deep copy

  1. Using JSON method:
let sqr = JSON.parse(JSON.stringify(area));
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
barely_adarsh profile image
Adarsh Naidu

Great one, thanks for sharing.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

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

Okay