DEV Community

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

Posted on

1

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️