Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
The ?? operator is like a smart fallback system! Unlike || which treats0,'', andfalse as "no value",?? only uses the fallback fornull and undefined!
count || 10 returns 10 even when count is 0! With count ?? 10, 0 is kept because it's a valid value.| Value | value || 'default' | value ?? 'default' |
|---|---|---|
null | 'default' | 'default' |
undefined | 'default' | 'default' |
0 | 'default' ❌ | 0 ✅ |
'' (empty string) | 'default' ❌ | '' ✅ |
false | 'default' ❌ | false ✅ |
NaN | 'default' ❌ | NaN ✅ |
'hello' | 'hello' | 'hello' |
Keep 0, false, and empty strings as valid values
Practical scenarios where ?? shines
Only null/undefined trigger fallback
Keeps 0, false, '', NaN as valid
Works great with optional chaining
Modern JavaScript - safer defaults