?? 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
undefined, else returns the left expression.
To know the difference between
- Not a Number keyword
- Empty string
- Number zero
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
value would equal the
rightSide if the
leftSide was ONLY
undefined false values.
Having said that, the
?? will return the same value as
|| in two cases:
- If the
leftSidewas a true value, both would return
- If the
undefined, both would return
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
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
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
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
otherChild is not defined in the
In this article, we knew that the Double Question Mark (Nullish Coalescing Operator) returns the right expression if the left expression was ONLY
After then, we knew that it is a special case of the OR Logical Operator, as it considers
"" 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:
- 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.