Question 15
Back to Exercise
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
const template = { title: "Report", pages: 10 };
const draft = template;
draft.pages = 20;
console.log("Template pages:", template.pages);
- A pages cannot be changed on a const object
- B draft = template copies the reference, so both names point to one object – copy it with { ...template } Correct
- C console.log reads the newest variable
- D Object properties are always shared between objects
Correct Answer: Option B
Assignment never copies an object. { ...template } (shallow) or structuredClone(template) (deep) creates an independent copy.
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!