DEV Community

Cover image for Nullish Coalescing - Let Falsy Fool You No More

Nullish Coalescing - Let Falsy Fool You No More

Laurie on December 10, 2019

Languages are constantly changing. And if you've read other posts of mine you know that I love checking in on the latest and greatest in JavaScript...
Collapse
 
silvestricodes profile image
Jonathan Silvestri

This combined with optional chaining is about to remove 75% of the reason I use lodash and I love it.

Collapse
 
lizraeli profile image
Lev Izraelit

Great examples for how valuable the ?? operator is! I think what makes it even better is that we can still use || in the few cases when we want to treat "" and 0 as false. In codebases where this becomes common practice, it will be easy to tell at a glance what values can be expected for the initial variable.

Collapse
 
laurieontech profile image
Laurie

Absolutely. Addition not replacement is key!

Collapse
 
aaron_powell profile image
Aaron Powell

Love this! I transformed our codebase at work with this along with Optional Chaining and it's so much more eloquent.

I also changed array.length > 0 checks for array?.[0] checks when we knew what the array was dealing with.

I do think it's worth mentioning that not all || situations will you want to convert to ?? as there are some very valid scenarios where treating falsy values as false is needed. :)

Collapse
 
laurieontech profile image
Laurie

Absolutely! And that's definitely something I could have/should have mentioned.

Collapse
 
daveskull81 profile image
dAVE Inden

This is rad. Nullish coalescing will be very useful. I’m looking forward to your write up on optional chaining.