- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Nullish Coalescing
JavaScript Objects
JavaScript Nullish Coalescing
The JavaScript nullish coalescing operator gives a fallback only when a value is
nullorundefined.
It is written as two question marks.
It is safer than ||, which also replaces 0, empty strings and false.
Example
Example
javascript
const count = 0;
console.log(count ?? 10);
console.log(count || 10);The output is 0 then 10.
|| replaced a perfectly valid zero.
What is Nullish Coalescing?
?? returns the right side only when the left is null or undefined.
