JavaScript · Junior · Question 14 of 75
How does count || 10 differ from count ?? 10 when count = 0?
- ✓Correct answer:
||returns10, while??returns0 - 2Both return
10, because0is falsy - 3Both return
0, because the value is defined
Explanation
|| substitutes the fallback for any falsy value, while ?? only does so for null and undefined. For numbers and strings where 0 and '' are valid values, ?? is the right choice.