What is Object Cloning in JavaScript ?

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.


Contact Us