Nullable Values and Safe Access
Nullish Coalescing
Nullish coalescing chooses a fallback only for null or undefined.
Nullish Coalescing
coalescing.ts
const displayName: string | undefined = ;
const fallbackName: string = "Guest";
const nameToShow: string = displayName ?? fallbackName;
console.log(`hello=${nameToShow}`);
const displayName: string | undefined = ;
const fallbackName: string = "Guest";
const nameToShow: string = displayName ?? fallbackName;
console.log(`hello=${nameToShow}`);
const displayName: string | undefined = ;
const fallbackName: string = "Guest";
const nameToShow: string = displayName ?? fallbackName;
console.log(`hello=${nameToShow}`);
nullish coalescing
The `??` operator keeps real values, but replaces `null` or `undefined` with a fallback.