Nullish Coalescing (??)
EasyIn plain terms
?? returns the right-hand side when the left is null or undefined. Unlike ||, it does not treat 0, "", or false as "empty" - only null and undefined trigger the fallback.
What you need to know
- •Returns right when left is null or undefined
- •Does not treat 0, "", false as empty
- •ES2020 feature
Try it yourself
Copy the code below and run it in your browser console or a code editor:
null ?? 'default'; // "default"
undefined ?? 'default'; // "default"
0 ?? 100; // 0 (0 is not nullish)
'' ?? 'default'; // "" (empty string is not nullish)
false ?? true; // false
// || treats 0, '', false as falsy
0 || 100; // 100
0 ?? 100; // 0
// Chaining
const value = a ?? b ?? c ?? 'final';Learn more
Dive deeper with these trusted resources: