DEV Community

Cover image for Logical OR (||) vs Nullish coalescing (??) operators
hemanth.hm
hemanth.hm

Posted on

Logical OR (||) vs Nullish coalescing (??) operators

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:

Alt Text

Original Post.

Top comments (1)

Collapse
 
t7yang profile image
t7yang

I think ?? should be |?, because we need &? too.