LearnFrenzy
Sign in Take a free test

Objects · Find the Error · Question 12

Bug: editing a draft changes the saved profile Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links