Nullish Coalescing with ??
Provide defaults only for null and undefined.
Providing Defaults
You often want a fallback when a value is missing. The nullish coalescing operator gives a clean way to supply defaults only when needed.
const name = undefined;
console.log(name ?? "Guest"); // "Guest"The ?? Operator
The double-question-mark operator returns the right-hand value only when the left side is null or undefined.
console.log(null ?? "fallback"); // "fallback"
console.log(undefined ?? "fallback"); // "fallback"
console.log("hi" ?? "fallback"); // "hi"All lessons in this course
- Optional Chaining with the ?. Operator
- Nullish Coalescing with ??
- Combining ?. and ??
- Optional Calls and Index Access