constuser={auth:{isLoggedIn:false}}constisLoggedIn=user.auth?.isLoggedIn||true// isLoggedIn would be true!constisLoggedIn=user.auth?.isLoggedIn??true// isLoggedIn would be false!
As you can see there is a difference in how they work.
The problem with || is that it will return the right value when the left value is falsy. The ?? just checks for null or undefined.
There are always alternatives if you can also write
if(user.auth?.isLoggedIn===true){// user is logged in!}
Nobody is forcing anybody to use the ?? operator but I think it just fits nicely together with the ?. and makes the intent clear if the developer knows how both of them work.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Good question!
Example time!
As you can see there is a difference in how they work.
The problem with
||
is that it will return the right value when the left value isfalsy
. The??
just checks fornull
orundefined
.Could also consider converting to a true/false value with:
@joshua I was literally going to say the same thing.
I fail to see the point/usefulness of the '??' operator
There are always alternatives if you can also write
Nobody is forcing anybody to use the
??
operator but I think it just fits nicely together with the?.
and makes the intent clear if the developer knows how both of them work.