LearnFrenzy
Sign in Take a free test

Destructuring · Predict the Output · Question 7

How many times does the default run? Answer, explanation & discussion

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

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

main.js
let calls = 0;
const makeDefault = () => (++calls, "default");
const [p = makeDefault(), q = makeDefault()] = ["given"];
console.log(p, q, calls);
  • A given default 2
  • B default default 2
  • C given default 1 Correct
  • D given undefined 0
Correct Answer: Option C

Defaults are evaluated only when the value is undefined. p receives "given", so its default never runs; q is missing, so makeDefault runs once.

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