LearnFrenzy
Sign in Take a free test

Destructuring · Predict the Output · Question 11

A parameter default object vs property defaults Answer, explanation & discussion

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

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

main.js
function f({ a = 1, b } = { b: 2 }) {
  return `${a}-${b}`;
}
console.log(f(), f({}), f({ b: 5 }));
  • A 1-2 1-undefined 1-5 Correct
  • B 1-2 1-2 1-5
  • C 1-undefined 1-undefined 1-5
  • D TypeError
Correct Answer: Option A

The default object { b: 2 } is used only when no argument (undefined) is passed. f({}) passes an object without b, so b is undefined; a always falls back to 1.

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