LearnFrenzy
Sign in Take a free test

Destructuring · Find the Error · Question 13

Bug: the default avatar is not used Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 13 Back to Exercise

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links