LearnFrenzy
Sign in Take a free test

Destructuring · Predict the Output · Question 20

Object defaults with null and missing keys Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 20 Back to Exercise

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

main.js
const { theme = "light", lang = "en", fontSize = 16 } = { theme: null, fontSize: 0 };
console.log(theme, lang, fontSize);
  • A light en 16
  • B null en 0 Correct
  • C light en 0
  • D null en 16
Correct Answer: Option B

Only lang is missing (undefined), so only lang uses its default. null and 0 are real values. Use ?? after destructuring if null should fall back too.

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