LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 19

Destructuring with a skip, a default and rest Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 19 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const [first, , third = "def", ...rest] = [1, 2, undefined, 4, 5];
console.log(first, third, rest);
  • A 1 undefined [ 4, 5 ]
  • B 1 2 [ undefined, 4, 5 ]
  • C 1 def [ 4, 5 ] Correct
  • D 1 def [ 2, 4, 5 ]
Correct Answer: Option C

The empty comma skips 2. The third element is undefined, so the default "def" applies. rest gets everything after it.

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