Question 12
Back to Exercise
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
const saved = { name: "Asha", address: { city: "Pune" } };
const draft = { ...saved };
draft.address.city = "Mumbai"; // user edits the draft only
console.log("Saved city:", saved.address.city);
- A Spread copies nothing
- B Spread makes a shallow copy, so address is shared – use structuredClone(saved) or copy address too Correct
- C const objects are always shared
- D city must be changed with Object.assign
Correct Answer: Option B
Only the top level is new. structuredClone(saved), or { ...saved, address: { ...saved.address } }, keeps the saved profile unchanged.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!