How to Use Nullish Coalesce for Real Defaults
?? only triggers on null/undefined. 0 and '' stay. || lies.

How to Use Nullish Coalesce for Real Defaults
?? only triggers on null/undefined. 0 and '' stay. || lies.

After working as a JS/TS developer for quite some years I've grown quite used to seeing || "" or ?? "" strewn around codebases as a quick fix to "remove" nullability, usually due to some value which you assume is set, but can't "prove" type-wise. I think this is a code smell, and I generally think it's not the right solution. In this post I try to explain why I don't think so, and provide some patterns that could help combat the downsides introduced by this pattern.
Abuse of the nullish coalescing operator in JS/TS
https://fredrikmalmo.com/blog/js-ts-nullish-empty-string-coalescing
#HackerNews #Abuse #of #the #nullish #coalescing #operator #in #JS/TS #JavaScript #TypeScript #NullishCoalescing #ProgrammingTips
After working as a JS/TS developer for quite some years I've grown quite used to seeing || "" or ?? "" strewn around codebases as a quick fix to "remove" nullability, usually due to some value which you assume is set, but can't "prove" type-wise. I think this is a code smell, and I generally think it's not the right solution. In this post I try to explain why I don't think so, and provide some patterns that could help combat the downsides introduced by this pattern.
TIL about the logical nullish assignment in JavaScript: