LearnFrenzy
Sign in Take a free test

Destructuring · Predict the Output · Question 14

Destructuring the first object of an array Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 14 Back to Exercise

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

main.js
const [{ name }, { name: second }] = [{ name: "Asha" }, { name: "Ravi" }];
console.log(name, second);
  • A Ravi Ravi
  • B Asha undefined
  • C SyntaxError
  • D Asha Ravi Correct
Correct Answer: Option D

Object patterns can sit inside an array pattern. The second one renames name to second to avoid declaring name twice.

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