JavaScript and React Tips and Tricks: Logical OR and Nullish Coalescing Operator Every JavaScript Programmer Must Know!
Tips and Tricks in JavaScript That Will Raise Your Level.
Preface
Nullish coalescing is an incredibly useful tool for developers. It can help you write shorter, more concise code, and in many cases, make your code more readable. Let’s take a look at a few examples below to see how nullish coalescing can help you write better code.
JavaScript contains what are called falsy and truthy values, each of which converts to a Boolean value.
There are 6 values that are considered falsy
values in JavaScript.
false Boolean(false) false
undefined Boolean(undefined) false
null Boolean(null) false
"" [empty string] Boolean("") false
NaN Boolean(NaN) false
0 Boolean(0) false
Therefore, if anything is not in the above list, then it will be considered a truthy
value.
Defined value: If a value is not undefined
or null
, it is considered a defined value.