LearnFrenzy
Sign in Take a free test

Destructuring · Predict the Output · Question 2

Skipping and rest on a string Answer, explanation & discussion

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

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

main.js
const [, x, ...y] = "hello";
console.log(x, y);
  • A e [ 'l', 'l', 'o' ] Correct
  • B h [ 'e', 'l', 'l', 'o' ]
  • C e llo
  • D e [ 'llo' ]
Correct Answer: Option A

A string is iterable by character. The comma skips "h", x gets "e", and the rest is collected into an array of characters.

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