Question 11
Back to Exercise
What does the following JavaScript code print (console output, as in Node.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!