In JavaScript, the Double Question Mark ??
is known as Nullish Coalescing Operator. It is introduced in the ECMAScript 2020 specification.
Simply, the Nullish Coalescing Operator is a logical operator that returns the right expression if only the left expression was null
or undefined
, else returns the left expression.
To know the difference between ||
and ??
operators in JavaScript, firstly, let’s remember what are the falsy values in JavaScript.
As we know, the falsy values in JavaScript are:
false
null
undefined
- Not a Number keyword
NaN
- Empty string
""
- Number zero
0
Great, look at this example:
By using the ||
in this example, value
will equal the rightSide
if the leftSide
was ANY false value.
However, if you used the ??
operator, value
would equal the rightSide
if the leftSide
was ONLY null
or undefined
false values.
Having said that, the ??
will return the same value as ||
in two cases:
- If the
leftSide
was a true value, both would returnleftSide
. - If the
leftSide
equaled eithernull
orundefined
, both would returnrightSide
.
Having said that, you can consider the Nullish Coalescing Operator ??
a special case from the OR Logical Operator ||
.
After introducing the difference between the Nullish Coalescing Operator and OR Logical Operator, you might end up thinking that one of them is redundant.
Let me argue with you by introducing the following use case of the Nullish Coalescing Operator.
1- Defining a default value for a variable
As you see, you can use both operators to provide a default value, however, based on your situation, you should determine which operator is more fit in your code.
For example, look at userInput2
, it might be acceptable for you if your client provides you with an empty string ""
, in such a case, you should use the ??
operator.
On the other hand, it is not acceptable for you if your client provided you with 0
number in the userInupt3
, in such a case, you should use the ||
operator.
And the same for userInput4
. It depends on your situation.
2- Defining a default value for a function argument
Like the previous use case, you can define a default value for a function argument. Have a look at the following example:
Here, we don’t use the ||
to define the default value, because 0
value is acceptable.
3- Using it as a short circuit
As you see, by using the ??
operator, the fun2()
will not be invoked at all because fun1()
doesn’t return null
or undefined
, however if so, both functions will be invoked.
4- Using it in combination with the Optional Chaining Operator (?.)
It is a perfect combination between the Optional Chaining Operator and the Nullish Coalescing Operator.
As you see, value2
is evaluated to be 'Default 2'
as the left expression returns undefined
as otherChild
is not defined in the parent
object.
In this article, we knew that the Double Question Mark (Nullish Coalescing Operator) returns the right expression if the left expression was ONLY null
or undefined
.
After then, we knew that it is a special case of the OR Logical Operator, as it considers 0
, false
, or ""
as true values.
Finally, we introduced some use cases of the Nullish Coalescing Operator.
If you liked this article please rate and share it to spread the word, really, that encourages me a lot to create more content like this.
If you found this article useful, check out these articles as well:
- 4 Ways To Handle Asynchronous JavaScript
- Liskov Substitution Principle Isn’t Complex. Just Give It A Try
- Isolation Levels In SQL Server With Examples
Thanks a lot for staying with me up till this point. I hope you enjoy reading this article.