[MUSIC].
>> Hi there. I'm Christopher Harrison.
I'm a Senior Program Manager inside of Microsoft Cloud and AI.
In this section, I want to take a look at how we can write if
statements in some Boolean logic inside of JavaScript.
So let's get right on into it.
So when it comes to Comparing values
or Boolean logic inside of JavaScript,
what you're going to notice is that
the operators for less than, greater than,
less than equal or greater than equal are pretty consistent
with all programming languages
that you can see them all right there,
and there's really no surprises.
But what you're going to notice though,
is that I've left equals off,
and the reason that I did that is because equals behaves a
little bit differently inside of JavaScript.
That one big thing to know about
JavaScript is the fact that it will
try to help you out as the Developer
by automatically doing data type conversions,
and while this might be something that you're
looking for, it's very infrequent.
Typically, this is not the behavior that we expect,
and so as a result,
this can actually lead to bugs in code.
So JavaScript gives us two different comparison operators.
We've got the double-equals and we've got the triple equals.
Now, in the case of the double-equals,
what's going to happen is it will
automatically do a conversion there.
So as a result, the string of 42
would actually be equal to the number of 42,
probably novel we were expecting.
But if we use the triple equals,
now those two values are not going to be
equal because one is a string and the other one is a number.
This is typically the behavior that we're expecting,
and so as a general best practice,
you should always use the triple equals.
That's going to be giving you what you
expect when you do your comparisons.
Now, as you also might expect,
are not equal operator,
we just simply put in a bang in
or exclamation point in front of the equals,
and again, that's going to be a regardless of data type.
Then the bang to equals is good check for
that as well as the data types.
So these are not equal operators,
and again, just like before,
our best practice is always
use the bang and then the double-equals after,
because that's really actually going to be all three.
So that will be checking for both the values
and the data types there,
again, that's typically what we want.
When it comes to the if statement logic,
the way that we build up an if statement is we say
if whatever our condition is inside of parentheses,
and then what we want to have happen on
the true side inside of that curly braces.
So in our case with our status being 200,
it's going to print out
console.log or it's going to run the code console that log,
and okay, so the printout would be okay in this case.
Now if it turned out that it was a value other than 200,
then it's going to start stepping down from there.
So we'd look at the next else-if, the next else-if,
then next else-if until it eventually
runs out of items and gets to that else.
That else at the very bottom winds up becoming a catch all there.
So if it's not 200 or 400,
then it would print out that
unknown status down at the very bottom.
It's worth highlighting here that in JavaScript,
similar to other languages like Java, C Sharp,
TypeScript, if what you're doing is a single line of code,
then you can actually skip the curly braces.
So I could actually in that case,
just simply write if status equals equals 200,
console that log okay.
Do my else-if right below it and do my else right below that,
and that actually gives me
the exact same logic as this right here,
just in slightly less space.
Now, some developers like this,
I personally don't use this because it
can actually lead to some potential errors later on.
So I typically don't use it.
I know some developers like it,
and that's why it's there.
So that way you can see other options.
The last one that I want to take a look
at what's known as the Ternary,
or potentially instant or inline if statement,
and so what this would do is it's
actually going to do our check right here in the middle,
you'll see that status equals 200.
This is going to be the check,
and if it turns out that it's true,
then it's going to give us back the okay.
Then if it turns out that it's false,
then it would wind up giving us back the error,
and that value would then wind up inside of message.
This is definitely one of those where it's better if we see it,
so let's get in,
write some code and take a look at all of this in action.