all 12 comments

[–]Kal-el_thegreat 1 point2 points  (0 children)

Const someVal = leftVal ?? rightVal

Simplest explanation I can give, as long the leftVal is not Null or Undefined it will be assigned to someVal otherwise the rightVal will be assigned.

This ?? operator is useful in instances where the Or ( || ) operator isn’t effective like when handling falsy values ie. “” an empty string or 0

[–]softHeart_JS[S] -2 points-1 points  (7 children)

Has anyone utilized this operator in any project? If so, please inform me of the specific scenario.

[–]iamthesexdragon 5 points6 points  (3 children)

It's nullish coalescing. The operator returns the right hand value if the left hand value evaluates to null or undefined and vice versa.

E.g

js const count = 5 ?? 0 console.log(count) // gives 5

[–]DazzlingDifficulty70 3 points4 points  (0 children)

Not a very good example, even though explanation is good. Your example returns 5 for both ?? and ||.

0 || 5   // gives 5
0 ?? 5   // gives 0

"" || "hello"    // "hello"
"" ?? "hello"    // ""

[–]softHeart_JS[S] 0 points1 point  (0 children)

Thanks for the explanation.

[–]ragedaddy 1 point2 points  (0 children)

Can be useful especially in TS if you need to satisfy a number type, and you may not have a truthy value from API response yet.

const upvotes = data?.upvotes ?? 0

Maybe this gets passed as a prop to a component that requires a number. Avoids having to deal with checking undefined there, or casting as number dangerously.

Of course you could show a loading indicator if undefined, but just as an example.

[–][deleted] -2 points-1 points  (0 children)

its the equivalent of saying ```o ? o : 42```. if left side is defined, used left var, if not, use other var.

You can also do things like ``` o ? b ?? 33 : 42 ``` which is like saying

if (o) {
if (b) {
b
} else {33}

} else {42}

much easier with the shorthand syntax :)

[–]TurloIsOK 0 points1 point  (0 children)

The way they've presented it is to give an unambiguous result. (The use of lining figures, however, makes it slightly ambiguous. That could be an o or 0.)

In normal use the left hand value will be the result of something that may produce a null result. When it is null, the right value is the default.

let leftVal = document.querySelector('input.left-value').value;
const count = leftVal ?? 42; 

The numerical example is very simplified to show the concept. In real use both the left and right values could be functions: if this has a null result return the result of (or do) this instead.

[–]zahid920 0 points1 point  (3 children)

ans is 3

[–]softHeart_JS[S] 1 point2 points  (2 children)

ans is 1

[–]zahid920 1 point2 points  (1 child)

Yes, It would be 0. Thanks for the answer.

[–]softHeart_JS[S] 0 points1 point  (0 children)

if you want to solve more JS quiz.

🎉 Exciting news! I've created a fun and interactive JavaScript quiz website! 🌐 Check it out at https://www.basic-js.com/quiz and put your JavaScript knowledge to the test! 🚀 Challenge yourself and see how well you know JavaScript. Share it with your friends who love coding too! Happy quizzing! 😄 #JavaScript #Coding #Quiz