LearnFrenzy
Sign in Take a free test

Destructuring · Predict the Output · Question 8

Defaults that use earlier variables Answer, explanation & discussion

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

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

main.js
const [a = 1, b = a * 2] = [];
const { x = 3, y = x + 1 } = { x: 10 };
console.log(a, b, x, y);
  • A 1 2 3 4
  • B 1 NaN 10 11
  • C 1 2 10 11 Correct
  • D undefined undefined 10 11
Correct Answer: Option C

A default may use variables that appear earlier in the same pattern. y's default uses the x that was actually received (10).

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