Whenever I talk about ??
people have asked me the difference between Logical OR (||) vs Nullish coalescing (??) operators, so here is a quick post.
Truth Table for logical OR ||
:
LHS || RHS
returns either of the truthy value.
LHS | RHS | Result |
---|---|---|
null | 1 | 1 |
undefined | 1 | 1 |
0 | 1 | 1 |
false | 1 | 1 |
'' | 1 | 1 |
`` | 1 | 1 |
NaN | 1 | 1 |
Truth table for Nullish coalescing ??
:
Returns the RHS for "nullish" LHS values.
Else returns RHS.
[nullish
-> null
or undefined
]
LHS | RHS | Result |
---|---|---|
null | 1 | 1 |
undefined | 1 | 1 |
0 | 1 | 0 |
false | 1 | false |
'' | 1 | '' |
`` | 1 | `` |
NaN | 1 | NaN |
Quick image with more details:
Top comments (1)
I think
??
should be|?
, because we need&?
too.