Daily Software Tips & Tricks
Bite-sized knowledge to improve your coding skills daily.
Ditch the OR Operator for True Default Values
February 2, 2026
If you're using JavaScript or TypeScript and setting default values with the standard OR operator (`||`), be careful. If the actual value is `0` or `false`, `||` will incorrectly treat it as 'falsy' and apply your default instead of preserving the valid value. Instead, switch to the Nullish Coalescing Operator (`??`). This operator *only* applies the default if the value is strictly `null` or `undefined`. It ensures that valid zero counts or boolean false values are preserved, making your logic much safer and less prone to subtle bugs.