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.