JavaScript Program to Clone a JS Object Using JSON.parse() Method Full Project For Beginners

 

 

script.js

 

 

// program to clone the object
// declaring object
const person = {
    name: 'John',
    age: 21,
}

// cloning the object
const clonePerson = JSON.parse(JSON.stringify(person));

console.log(clonePerson);

// changing the value of clonePerson
clonePerson.name = 'Peter';

console.log(clonePerson.name);
console.log(person.name);

Leave a Reply