const settings = { count: 0, title: "" };
console.log(
settings.count || 10,
settings.count ?? 10,
settings.title ?? "Untitled",
settings.user?.name
);
10 0 "" undefined
The answer is 10 0 "" undefined.
• || falls back on any falsy value, so 0 becomes 10.
• ?? falls back only on null or undefined. So 0 stays 0, and '' stays ''. The console shows the empty string as a blank.
• Optional chaining ?. stops and returns undefined when settings.user is missing. It does not throw a TypeError.
The tempting option "10 0 "Untitled" undefined" is wrong because '' is not null or undefined, so ?? keeps it.
Read more