Question 13
Back to Exercise
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
const profile = { name: "Ravi", avatar: null }; // the API sends null for "no avatar"
const { avatar = "/img/default.png" } = profile;
console.log(`<img src="${avatar}">`);
- A Template literals turn undefined into null
- B Defaults cannot be strings
- C avatar must be renamed
- D Destructuring defaults apply only to undefined, and the API sends null – use const avatar = profile.avatar ?? "/img/default.png" Correct
Correct Answer: Option D
null is a value, so the default is skipped. ?? treats both null and undefined as missing.
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!