Beginner JavaScript Interview QuestionsBeginnerComparison
JavaScript · Question 24
What is the difference between the nullish coalescing operator (??) and logical OR (||)?
Direct answer
?? uses the fallback only for null or undefined, while || uses the fallback for any falsy left-hand value.
This difference matters when 0, false, or an empty string is a valid value. For example, 0 || 10 evaluates to 10, while 0 ?? 10 evaluates to 0.
A common pattern is user?.settings?.pageSize ?? 20: optional chaining safely reads the property, then nullish coalescing provides a default only when the property is actually missing/nullish.