Description: This method creates a deep copy of an object, meaning that nested objects are also cloned. However, it has limitations such as not supporting functions, undefined
, NaN
, Infinity
, and circular references.
Example:
const original = {
name: 'Max',
age: 6,
color: 'black',
nested: {
a: 1,
b: 2
}
};
const copy = JSON.parse(JSON.stringify(original));
console.log(copy); // Output: { name: 'Max', age: 6, color: 'black', nested: { a: 1, b: 2 } }
copy.nested.a = 10;
console.log(original.nested.a); // Output: 1 (original nested object is not affected)