typescript / beginner
Snippet
Nullish Coalescing (??)
The nullish coalescing operator (??) returns the right-hand operand only when the left-hand operand is null or undefined.
snippet.ts
1
2
3
4
const userConfig = null;const finalConfig = userConfig ?? "Default Setting";console.log(finalConfig);
Breakdown
1
userConfig ??
Checks if userConfig is null or undefined.
2
"Default Setting"
The fallback value used if the check on the left succeeds.
3
null
Since the value is null, finalConfig will become 'Default Setting'.