Nullish Coalescing and when you should use it
The ??
operator is identical to ||
when the left side is null
, undefined
, or truthy
.
They differ when the left side is falsy
but not null
or undefined
.
In this case:
??
returns the left side.
||
will returns the right side.
In practical terms, ??
returns values with an operable type (string
, number
, bigint
, boolean
, etc.) even when their values are falsy.
falsy
contains nullish
falsy
is a superset of nullish
.
It’s a square, rectangle, paralellagram situation.
Examples
Number
String
Boolean
Nullish