LearnFrenzy
Sign in Take a free test

Spread and Rest Operators · Find the Error · Question 14

Bug: a new tag becomes letters Answer, explanation & discussion

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

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

main.js
const tags = ["js", "css"];
const newTag = "react";
const updated = [...tags, ...newTag];
console.log(updated.length, updated.join(","));
  • A Spreading a string adds its characters – add the string itself: [...tags, newTag] Correct
  • B Arrays cannot contain strings
  • C join adds the commas
  • D tags must be spread twice
Correct Answer: Option A

Strings are iterable, so ...newTag expands to "r", "e", "a", "c", "t". Only spread values that are collections you want to unpack.

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