Sub topic 3
Nullish Coalescing with ??
This chapter assumes you already know which values are falsy and that || and && return one of their operands instead of always returning true or false.
Nullish Coalescing with ??
The ?? operator returns its right operand only when the left operand is null or undefined. JavaScript calls these two values "nullish." Other falsy values, such as 0 and "", are kept.
const settings = { volume: 0, name: "" };
console.log(settings.volume || 50); // 50 (0 is lost)
console.log(settings.volume ?? 50); // 0
console.log(settings.name ?? "Anonymous"); // ""
console.log(settings.theme ?? "light"); // "light"Use ?? when 0, "" or false count as real values.
Optional Chaining and Safe Property Access
The ?. operator stops evaluating and returns undefined when the value on its left is nullish. Because it returns undefined, it combines well with ??:
const user = { profile: null };
console.log(user.profile?.email); // undefined
console.log(user.profile?.email ?? "no email"); // "no email"
const callback = undefined;
callback?.(); // no call and no TypeErrorLogical Assignment: ||=, &&= and ??=
Each logical assignment operator assigns only when its operator would evaluate the right side:
const opts = { retries: 0, label: "" };
opts.retries ||= 3; // retries becomes 3
opts.label ??= "default"; // label stays ""
opts.timeout ??= 1000; // timeout becomes 1000
let token = "abc";
token &&= token.toUpperCase(); // "ABC"
console.log(opts, token);Precedence Rules and Why ?? Needs Parentheses
You cannot mix ?? directly with || or &&. JavaScript throws a SyntaxError unless you add parentheses:
// const x = null || undefined ?? "default"; // SyntaxError
const x = (null || undefined) ?? "default";
console.log(x); // "default"Guard Patterns and Common Pitfalls in Real Code
A guard written with && returns the falsy operand itself. That can put a stray 0 in your output:
const items = [];
const label = items.length && `${items.length} items`;
console.log(label); // 0, not false
const safeLabel = items.length > 0 && `${items.length} items`;
console.log(safeLabel); // falseCompare explicitly when the left side could be 0.
Truthy Is Not the Same as == true
Loose equality == converts both sides to numbers. Truthiness follows different rules:
console.log([] == false); // true
console.log(Boolean([])); // true
console.log("0" == false); // true
if ("0") console.log("\"0\" is truthy"); // logsTo check whether a value is falsy, write if (!value) instead of value == false.
The document.all Oddity
Exercise
What do 0 ?? 10, 0 || 10, and ({ a: null }).a?.b ?? "none" evaluate to?
Show answer
0, 10, and "none". ?? keeps 0 because 0 is not nullish. || replaces it because 0 is falsy. ?. returns undefined at the null value, so ?? falls back to "none".
Exercise
Given let count = 0;, what is count after count ||= 5; runs, and what is it after count ??= 5; runs instead?
Show answer
After ||=, count is 5, because 0 is falsy. After ??=, count stays 0, because 0 is not nullish.