LearnFrenzy
Sign in Take a free test

Data Types · Find the Error · Question 15

Bug: changing the "copy" changes the original Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 15 Back to Exercise

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

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

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links