capypad
0 day streak
typescript / beginner
Snippet

Nullish Coalescing Operator

The nullish coalescing operator (??) returns the right-hand value only if the left-hand value is null or undefined. Unlike the OR (||) operator, it doesn't trigger for empty strings or 0.

snippet.ts
typescript
1
2
const input: string | null = null;
const result = input ?? 'Default Value';
Breakdown
1
input ?? 'Default Value'
Evaluates to 'Default Value' because 'input' is null.