capypad
0 day streak
typescript / beginner
Snippet

Nullish Coalescing

The nullish coalescing operator (??) returns its right-hand operand when its left-hand operand is null or undefined. Unlike ||, it treats 0 as a valid value.

snippet.ts
typescript
1
2
const settings = { volume: 0 };
const volume = settings.volume ?? 50;
Breakdown
1
settings.volume ?? 50
Uses 50 only if volume is null/undefined. Since volume is 0, it keeps 0.