. Advertisement .
..3..
. Advertisement .
..4..
As in the process of inspecting the code, many of us must have encountered the double question mark Javascript no less than once. If, by any chance, you get confused by this sort of character, just know you are not the only one.
That is why here we are to get you out of trouble. Read on to learn more!
What Does Double Question Mark Javascript Represent?
In Javascript ES2020 standard, the double question mark is the indicator of the Nullish Coalescing operator. This is a new function of such a version, which lets you set a default value so that it can be of use when a variable expression is evaluated as undefined or null.
That way, once this logical operator finds that one of the two accepted values is committing the fault above, it will return the other on the right side of the mark.
Have a look at the code below. Let’s say the firstName is assigned as the username variable value, and the code inspects that this value is undefined. Then the operator will automatically assign it to “Visitor”.
let firstName = undefined;
let username1 = firstName ?? "Editor";
console.log(username1); // "Editor"
// You may also write it like this:
let username2 = undefined ?? "Visitor";
console.log(username2); // "Visitor”
Why Should You Use The Nullish Coalescing Operator?
Little did you know, we can employ Nullish Coalescing exactly like the way we employ the logical OR operator. Then why should we utilize the former but not the latter?
The logical OR operator (||) is often practical in assigning a default value as variable. For instance:
let a;
let result = a || 19 ;
console.log(result); // 19
As such, we have the null variable is “a”, which is compelled to go false, triggering the result to become 19.
Sounds not much different from the double question mark javascript, doesn’t it? Nevertheless, there is a risk to making use of this operator: It may arise some unlooked-for outcomes if you regard certain values, such as 0 or “, to be in the case.
For instance, we have the code as follow:
const value1 = 0 || 'double question mark Javascript string';
console.log(value1);
const value2 = '' || 1997;
console.log(value2);
Output:
double question mark Javascript string
1997
In variable value1, the code features “0” and “default string.” Let’s say we report its value in the console; what we receive will be the “default string“, which is completely strange.
Since zero is neither undefined or null, we should get “0” instead. As can be seen, ‘||’ is not much effective in this situation.
So, what happen if we substitute ‘||’ by ‘??’? Here is the upcoming scenario:
const value3 = 0 ?? 'double question mark Javascript';
console.log(value3);
const value4 = '' ?? 1998;
console.log(value4);
Output:
0
To put it another way, “??” permits only undefined and null values, which make it work wonders for those empty strings (“) or 0s.
Conclusion
We hope that the above tutorial can help you grasp more about the double question mark Javascript by now. It’s then the time for you to grab the keyboard and idealize your math. Wish you the best of luck!
Leave a comment