LearnFrenzy
Sign in Take a free test

Destructuring · Predict the Output · Question 12

Destructuring a string as an object Answer, explanation & discussion

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

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

main.js
const { length, 0: first, [length - 1]: last } = "Kiran";
console.log(length, first, last);
  • A 5 K undefined
  • B undefined K n
  • C ReferenceError
  • D 5 K n Correct
Correct Answer: Option D

The string is boxed, so length and index properties can be read. Properties are processed left to right, so length is already initialised when the computed key [length - 1] is evaluated.

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