>> Hi there. I'm Christopher Harrison,
Senior Program Manager inside of Microsoft Cloud and AI,
and I want to keep on keeping on talking
about Boolean Logic and other options,
like Switch, that are available to us inside of JavaScript.
So let's start off by talking a little bit about
implicit conversions to Booleans inside of JavaScript.
Now this something that a lot of programming languages do,
including say like Python,
where it will look at a variable and if it's
storing the default value for that data type.
An empty string in case of a string,
a null value in case of an object,
or zero in the case of a number.
All of those will automatically test as false.
So this can be a really quick way for
us to check to see whether or not a variable is storing a value.
With that logic there,
it would not print out X contains a value because X is zero.
Now, just like with our equality operator,
we can also still use the exclamation point,
or "bang" to reverse that.
If I take that logic and I put a "bang" in front of the X,
now what's going to happen is I'm looking to see whether or not
this does not contain a value.
Because it's going to convert that X into a Boolean,
so it would be false,
and then it's going to reverse that into true.
In this case, it's looking to
see that it contains
no value and that's what it's going to display.
Just like I tripped over my words there,
it's a good reminder that when you're writing your code,
you typically want to look for the
positive rather than looking at the negative.
Finally, strings in JavaScript,
when we're doing our comparison,
are going to be case sensitive.
So in this particular case,
where I've got my name equals Christopher, lowercase c,
and then I'm going to do my comparison with a capital
C. Those two strings are not going to be equal.
You can handle this by either converting them
to all upper or all lowercase,
or you could also take a look at something like locale compare,
which you can look inside of
the supporting documentation for
a little bit more information on that.
Also, just like other programming languages,
we could combine comparisons by using the "and" operator,
which is the ampersand,
or the "or" operator, which is the pipe.
Keep in mind that with "and",
both sides need to be true in order for that to
evaluate as true and with "or"
only one side need be true in order for that to evaluate as true.
One thing that you might have noticed is the fact that we've
got a single and a double of each one of those.
You might be wondering what's the difference?
The double is going to be known as
a shortcut operator and what that will
do is it will actually skip the second side,
if it turns out that the first side gave it the answer.
In the case of an ampersand,
which is our "and",
what's going to happen is that if the first value is false,
it's going to skip evaluating the second.
Why? Because if that first one is false,
the entire thing is guaranteed to be false.
They have to both be true in order for it to be true.
If one's false, we've already failed it.
The same is going to hold true with our worse statement.
The difference is the fact that if the first one is true,
now we know it's going to test as true,
and so it will wind up skipping that one.
As a general rule,
you want to use that ampersand and
pipe pretty much always
because it's always going to be what you want.
I've only run into a couple of
cases where what I wanted was the single one.
If we take a look at how we can combine these,
you're going to notice on our "else-if" that I've got my
"or" so if the status is 400 or 500,
then we're going to print out "error".
You might be looking at that and you might be going,
there could be a lot of different values here that I want to
go in and test for and do different things on,
and I could wind up with a really long statement here.
This is where Switch,
or as some people like to call it,
a case statement or maybe even just Switch case, comes into play.
That with a Switch statement,
what we'll do up at the very top is
indicate this is the value that we're going to test.
We're going to test our status and we're going to look
to check for all the possible cases,
all the possible values.
Could it be 200?
Could it be 400?
Could it be 500?
The couple of things that I want you to notice here,
number one is the fact that it's
always going to check for equality.
I'm not able to do "less than" or "greater than" here.
Fortunately, one thing that I can do is stack one after the other.
That second block that you're seeing there of that case 400,
case 500, that's basically like an "or".
If it's 400, it would run the code;
and if it's 500, it would run the code.
One big thing to keep in mind about writing
Switch statements is that that break statement is required.
If you don't have that break statement,
what's going to happen is it will
perform what's known as a silent fall-through,
meaning that it will keep running all of the logic that it sees.
Without that break statement,
what would wind up being printed out on
the screen would be "okay",
"error", and if we didn't have that second break statement,
it would even print out unknown value, silent fall-throughs.
Typically not what you're going to be looking
for when you are writing your Switch statements.
Let's get in and take a look at
the syntax here and hopefully bring all of
this together for you so you can see how our Boolean operators,
our various bits of logic work inside of JavaScript.