Open In App

What is Object Cloning in JavaScript ?

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Object cloning in JavaScript refers to creating a duplicate object from an existing one. There are two types:

Shallow Cloning

Copies only top-level properties. Nested objects are copied by reference.

const clonedObject = { ...originalObject };

Deep Cloning

Copies all properties and nested objects recursively.

const clonedObject = JSON.parse(JSON.stringify(originalObject));

Shallow cloning is sufficient for simple objects, while deep cloning is needed for complex ones. However, deep cloning with JSON.stringify() and JSON.parse() has limitations, such as losing functions and circular references.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads