Question 18
Question
Answer
function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}const originalObject = {
name: "Alice",
age: 30,
address: {
street: "123 Main St",
city: "Anytown"
}
};
const copiedObject = deepCopy(originalObject);
copiedObject.name = "Bob"; // Change the name in the copy
console.log(originalObject); // Still outputs: { name: "Alice", age: 30, ... }
Last updated