LearnFrenzy
Sign in Take a free test

Destructuring · Predict the Output · Question 18

An object pattern on an array Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 18 Back to Exercise

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

main.js
const { 0: a, 2: c, length } = ["x", "y", "z"];
console.log(a, c, length);
  • A x y 3
  • B undefined undefined 3
  • C x z 3 Correct
  • D TypeError
Correct Answer: Option C

Arrays are objects whose indexes are property keys, so an object pattern can pick indexes and length directly.

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