JavaScript Nullish Coalescing and Logical Assignment
Quick answer
Nullish coalescing ?? selects a fallback only when its left operand is null or undefined. It preserves zero, false and empty strings.
How it works
value ?? fallback evaluates fallback only for nullish values. Logical assignments ??=, ||= and &&= write only when their corresponding short-circuit condition calls for the right operand. The left assignment target is evaluated once. A direct unparenthesized mix of ?? with && or || is a syntax error.
console.log(0 ?? 10);console.log(0 || 10);console.log(null ?? 10);Expected output in Node.js:
01010Zero is falsy but not nullish.
let limit = null;limit ??= 5;let label = "";label ||= "Untitled";let enabled = true;enabled &&= false;console.log(limit, label, enabled);Expected output in Node.js:
5 Untitled falseEach operator applies its own condition before writing.
Common mistakes
Choose a fallback based on the meaning of missing data. Nullish coalescing is not a validity check: NaN, negative numbers and empty text remain unchanged.
Practice
Return the fallback 10 only if the input is null, preserving valid zero and false.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Nullish Coalescing and Logical Assignment — Knowledge Check
Apply the taught concept and reason about the result.
What is 0 ?? 8?
What is false ?? true?
After let x=null; x??=4; what is x?
After let x=0; x ||= 9; what is x?
Which grouping is valid for combining nullish coalescing with OR?