Let’s suppose you have the following:
|
|
How can you reduce it to one line without writing if
?
|
|
OK, you gain 3 lines, but before you use it everywhere, think about 2 things for your use cases:
- Is it changing something to the performance of your application?
- Is it going to be as readable as the 3 lines to anyone, senior and junior?
Maybe, in the example, it’s OK to use the Nullish coalescing operator.
I would say it isn’t always the case.
Please read the MDN article on the Nullish coalescing operator on the topic.
Also check out this javascripttutorial.net article which I liked because it goes in detail with nice use cases.
Thanks for reading.