LearnFrenzy
Sign in Take a free test

Arrays · Find the Error · Question 13

Bug: the backup changes too Answer, explanation & discussion

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

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

main.js
const todo = ["email", "report"];
const backup = todo;
todo.push("meeting");
console.log("Backup:", backup);
  • A push adds to every array
  • B const arrays are shared automatically
  • C backup = todo copies the reference, so both names show the same array – copy with [...todo] Correct
  • D console.log prints the newest array
Correct Answer: Option C

Arrays are objects. const backup = [...todo]; (or todo.slice()) keeps a separate copy with two items.

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