DEV Community

Cover image for 6 Nullish coalescing operators every javascript programmer must know !

6 Nullish coalescing operators every javascript programmer must know !

sudarshan on May 03, 2021

TL:DR : Most of the time out javascript code becomes clumsier if we inserted the multiple null checks for the several entities. Those ch...
Collapse
 
cariehl profile image
Cooper Riehl

Good article!

I appreciate how you built up to the (in my opinion) most useful forms of null-checking, the ?. and ?? operators. Coming from a .NET background, I use this pattern frequently, so it's good to know that I can apply the same pattern in JavaScript.

Thanks for sharing :)

Collapse
 
sudarshansb143 profile image
sudarshan

Greatful for your feedback !

Collapse
 
jinsasa profile image
Christian Grauer

I like this post very much, especially the "?. with ??" thing, which I didn't know before. Is there any similar thing for use with the bracket-syntax (like for person[itsName][myHobby])?

Collapse
 
bhargavshah profile image
Bhargav Shah

Great job compiling this list!
Just want to point out the word operator in the title is misleading. There is only one nullish coalescing operator that is ??. These are all great ways to handle nullishness, but they are not all "operators".
Cheers!

Collapse
 
sudarshansb143 profile image
sudarshan

Thanks :)

Collapse
 
gleaming_cube profile image
Darren Harding

Great post. My personal preference would be to use .some() instead of .includes() as it will return early as soon as it resolves true, where .includes() will go through every item in the array when it probably doesn't need to. I would also agree that nested ternary is very hard to read so I would probably avoid that if I had to nest multiple conditions and just fall back to good old fashioned if/else.

But this is really useful, thank you.

Collapse
 
sudarshansb143 profile image
sudarshan

Thanks for your feedback !

Collapse
 
sebalr profile image
Sebastian Larrieu

Good post but I don't agree that multiple ternary operators is a good idea. There even is a lint rule to prevent that. In my opinions it makes code harder to follow that if-else

Collapse
 
sudarshansb143 profile image
sudarshan

Thanks for you feedback !

but, I think It is preferential to whether or not we should use this kind of stuff !

I will definitely look into that !

Collapse
 
kwadoskii profile image
Austin Ofor

Super intuitive and neat.

Collapse
 
sudarshansb143 profile image
sudarshan

😇

Collapse
 
metalmikester profile image
Michel Renaud

I knew (and use) two of these but didn't know about default assignment and includes. That could come in handy! Thanks! :)

Collapse
 
sudarshansb143 profile image
sudarshan

Thanks !