BeginnerCoder

Lesson 11 — Nullish & optional deep

The right default

The || operator replaces every falsy value (a value that counts as false: 0, "", false, null, undefined). That is a bug when 0 is real data. The nullish coalescing operator ?? only replaces null and undefined, so a real 0 survives. Combine it with ?. for deep, safe reads.

const stock = 0;
console.log(stock || 10); // 10  (wrong: 0 was real)
console.log(stock ?? 10); // 0

Why it matters: "0 items", "0 points" and empty strings are valid values, and ?? keeps them instead of hiding them.

Want it another way?
Task

Fix the log line: a missing profile must print unknown (no crash), but Max's real age 0 must print 0.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.