??) is a logical operator known as the Nullish Coalescing operator. This operator returns its right-hand side operand when its left-hand side operand is
undefined, otherwise it returns the left-hand side operand.
The Nullish Coalescing Operator (
??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand. This can be contrasted with the logical OR (
||) operator, which returns the right-hand side operand if the left operand is any falsy value, not only null or undefined.
In the first example, the
?? operator sees
0 as a valid value and therefore returns it. In the second example, the
|| operator sees
0 as a falsy value and therefore returns
?? operator is particularly useful when dealing with values that may be
undefined. It allows for setting default values in a more intuitive way than using the
In this example, the user's theme preference is
null, so the
?? operator returns the default theme.
The ternary operator (
In this example, if the value is greater than
0, the string
"Positive" is logged. Otherwise, the string
"Non-positive" is logged.
All modern browsers support the Nullish Coalescing Operator (
Yes, the double question mark operator is supported in all modern browsers. However, it may not be supported in older versions of Internet Explorer.
No, chaining multiple double question marks together is not supported and may result in a syntax error. The operator is intended to handle a single operand for nullish coalescing.
If both expressions in the double question mark operator are null or undefined, the operator will return the value of the left-hand side expression.
Join our community of forward-thinkers and innovators. Subscribe to get the latest updates on courses, exclusive insights, and tips from industry experts directly to your inbox.