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.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay