The differences between logical or (||) and nullish coalescing (??) operators
October 17, 2024
Both operators are used to assign default valuesut they differ in how they determine what is considered a “falsy”.
-
Logical or (
||): Returns the right-hand if the left-hand operand is falsy (false,0,'',null,undefined, andNaN). -
Nullish coalescing (
??): Returns the right-hand operand if it is notnullorundefined.
const a = 0
const b = a || 5 // b will be 5, since 0 is falsy.
const c = a ?? 5 // c will be 0, since 0 is not null or undefined.