Double Question Mark in JavaScript

The Double Question Mark (Nullish Coalescing Operator) in JavaScript

In JavaScript, the Double Question Mark (Nullish Coalescing Operator) is a special case from the OR Logical Operator (||). Let’s introduce some use cases of it.

Introduction

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.

What is the Difference Between Double Pipe and Double Question Mark in JavaScript?

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 return leftSide.
  • If the leftSide equaled either null or undefined, both would return rightSide.

Having said that, you can consider the Nullish Coalescing Operator ?? a special case from the OR Logical Operator ||.

Use Cases of Nullish Coalescing Operator in JavaScript

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.

💌 Enjoying this post? Subscribe to get more practical dev tips right in your inbox.

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.

Conclusion

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.

Think about it

If you enjoyed this article, I’d truly appreciate it if you could share it—it really motivates me to keep creating more helpful content!

If you’re interested in exploring more, check out these articles.

Thanks for sticking with me until the end—I hope you found this article valuable and enjoyable!

Want more dev insights like this? Subscribe to get practical tips, tutorials, and tech deep dives delivered to your inbox. No spam, unsubscribe anytime.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top