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.

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay