[MUSIC].
>> Hi there. I'm Christopher Harrison,
Senior Program Manager inside of Microsoft Cloud AI.
This time I want to take a look at syntax
for if statements inside of JavaScript.
I've got an empty little file here.
Let's start filling it up with a bit of code.
I'm going to start off by saying status equals 200.
Let's start coming up with our different display messages here.
I'm going to say if status equals 200,
and then we'd have the true side here.
In our case I'm going to print out,
okay, which I'll get to in a second.
But I want to one more time highlight this fact.
We want to avoid using double equals inside of JavaScript.
Because again, what's going to happen is if I do this,
turn 200 into a string,
and I'd say console dot log okay and I run this,
and I go ahead and I run my code,
it's going to print, okay.
But things are decidedly not okay because
we shouldn't have a string and an integer be equal.
My triple equals, there we go, will prevent that.
Now if I run this, I'm going to get no message.
Now, let's go ahead and give ourselves a message here real quick.
I'm going to say else and then we'll go
ahead and say console dot log,
unknown status, just like that, save rerun.
Now you're going to notice that it gives us that unknown status
because strings don't equal numbers.
I'd have to convert that in order for that to be true.
Well, let's go ahead and just punch in 200 here.
Now we're going to be back to, okay, fantastic.
Now let me sneak down
here and I'm going to say else if and then do another comparison.
I'll say status equals,
equals, equals and let's say 400.
Then we'll go ahead and say
console.log and then we'll say error like that.
Now if I run this and I run this as is,
what we'll notice is the fact that
it's just going to give me, okay,
just like before, because it equals
200 and so that part right there is going to capture that.
If I change this to 400 and now I rerun this,
now we'll see that it's going to give me error.
That's how our if statements
or else statements and our else ifs are
going to work in the more traditional way of doing this.
Now, I also do have the ability to be a little bit more terse.
That instead of this full blown syntax here,
let me put this over here and we can see this now side by side.
We'll just go ahead and copy and paste the code here,
is because each one of these messages is single line,
I don't need these curly braces,
so I can go ahead and get rid of them.
My code is going to,
let's make sure I put in my semi-colons there we go,
my code is going to behave the exact same way.
Let me see if I can't.
I'm going to need to shrink my text just
a little bit, there we go.
That looks better, cool.
Now what you're going to notice over on
the left-hand side here is it's the same logic.
It's just now without the curly braces.
But again, you do want to be careful about that because
it can lead to unexpected errors.
It will behave the exact same way.
Let me save this and I'm going to say node if terse,
run it, same thing, same as before.
But we do wind up with
that little challenge of
there potentially being other error messages.
Personally, I'm not a fan of that but some people are.
I'm not going to take a stance.
Let's close it all out by taking a look at the ternary statement.
Let's start off by doing this.
I'm going to say console status equals 400.
I'm going to do this, I'm going to say let
message equal to an empty string.
Now I'm going to say if status equals, equals,
equals 200, then our message equals okay.
Otherwise, if I can spell else, there we go.
Our message will equal error will just say error.
Then we'll say console.log message.
Now let me go ahead and save that.
Let's node if ternary and then it gives us error.
Of course if I change that to be 200,
it would give us okay.
Now, that's a lot for a real quick check.
I can name that song and OneNote, if you will,
consolidate all of that down by saying const message equals.
Let's put an R check so we'll say status is 200.
If that's the case,
then we're going to say okay, otherwise oops.
We're going to say error, beautiful.
Now our code is going to give us the exact same result here.
It's just down on one line.
Now, I will openly admit that that's one
of those bits of syntax that I frequently
need to go back and double-check because
it's not one that I use real often.
But what I do like about it is the fact
that it allows me to avoid having to
declare message on the outside
and then set it to different things in
the middle that I can declared as const,
it will be set ones.
Then I don't have to worry about like
little scoping things or otherwise.
Those are the different ways that we can write
our if statements inside of JavaScript.