Are you still using the OR operator (||) to set default values in JavaScript? While it's a common practice, it has a hidden issue: the OR operator treats values like 0, "", and false as missing. This means your fallback value is triggered even when the original value is valid. A safer alternative is the Nullish Coalescing Operator (??). This operator only activates when the value is null or undefined, providing more predictable behaviour. Here's a quick comparison: - Using the OR operator: const score = input || 10; If input = 0, this unexpectedly becomes 10. - Using Nullish Coalescing: const score = input ?? 10; Here, if input = 0, you get 0 as intended. For cleaner and more reliable defaults in your code, consider using ??. It does exactly what you mean. #javascript #frontendengineering
This is one of those subtle gotchas that confuses so many junior devs at the start. Really clear breakdown, hopefully this helps a bunch of people avoid that frustration.
Great point — the || vs ?? difference trips up so many devs. I remember hitting this exact bug when handling a 0 price value — couldn’t figure out why it kept defaulting to 10 😅. Ever since switching to ??, my defaults finally behave the way I expect. Small change, huge reliability boost.