Welcome back.
Let's talk about.
Knowledge coalescing operator, and it's this double questionmark.
So remember, we learned about the big event and we also learned about the optional training with the
questionmark and the period just like this.
So what's this double questionmark and this double questionmark can often be used instead of what we've
seen before, the or operator.
So let me show you what I mean.
Let's imagine that we have a new property here and we'll call it power.
And power for Pikachu is.
Now, if I wanted to get the power of my Pokémon, then I can simply do this and grab the power or the
property power lining.
But let's say we weren't sure that we had this property for the Pokémon, so we can't really do questionmark
period because, well, that doesn't really make sense.
Remember, the optional chaining operator works on objects and checking their properties, but power
is the end of the chain.
We can't really do anything.
So most of the time what we would do is do an hour operator.
So if let's say Pikachu doesn't have a power, we can just say no power.
So let's imagine here that we have an empty string or maybe not even the property, and if I copy this
over and run this.
I get no power that makes sense, right?
But what if we had power in here?
But it's an empty string.
What would happen then?
Well, if I run this again.
I get no power again.
And that is because this or operator works by checking if this is Truthy, if it's not Truthy, then
it resorts to no power.
That's how JavaScript works.
So this empty string evaluates as false and I get no power.
What if the power was false?
Once again, this will resort to force and you'll get no power, but what if we wanted this to be an
empty string instead of no power?
We want to check if this is no or undefined.
Only then do we want to give no power.
Otherwise, maybe this Pikachu just is still evolving and doesn't have any power.
But we don't necessarily want to say no power.
Well, in that case, we can do the knowledge coalescing.
And this instead of the OR operator doesn't check if a value is falsey.
Instead, it checks if it's null or undefined.
So things like zero, for example.
If we do that, well, let's find out.
If I copy this, I get power of zero.
If this was an or.
In that case, the result would be different.
The result would be no power, even though we actually have a value.
So, again, knowledge coalescing is really, really useful for those occasions that you might use the
or operator.
Pretty, pretty cool.
All right.
I think that's enough.
There's a few more to cover with 20.
But again, we're going to encounter those throughout the rest of the sections because they require
a bit of other knowledge that we're going to accumulate.
So let's take a break and I'll see you in the next one.