Welcome back.
So in this lesson we're going to pick up right where we left off and we're going to see how do we change
together simple or boolean statements like the one we just saw into more complicated statements that
have multiple components.
So they're still all going to evaluate to be true or false at the end of the day.
But there might be multiple pieces that individually evaluate to be true or false before the entire
thing becomes true or false.
So there are three logical operators and like we said this is this is basically a way to chain together
fully in logic.
So those three are called AND OR and NOT.
So we're going to start talking right.
And and the way that works.
First of all you write it with two ampersands and you can see right here we're using it in an example
where x is equal to 5 and Y is equal to 9.
So and takes two sides.
There is a left side and a right side and goes in the middle.
So the way you would read this is X less than 10 and X not equal to 5.
So we'll evaluate both sides.
X is less than 10.
That is true.
Five is less than 10 x is not equal to 5 and that is false.
So we end up with true and false.
And the way that works is that it requires both sides to be true.
It requires both of them to be true Left and Right and that's not true in this case only the left side
is true.
So the entire thing is false.
The way that or works is slightly different it still takes both sides.
But it only requires that one of them be true left or right be true in order for the whole thing to
be true.
Just a side note the character here the pipe character is located above most peoples enter or return
key and you usually need to hit shift to make it work.
So two of those is how you write or in Javascript.
So the Left Side Y is greater than nine.
That is not true because Y has 9 or X is triple equal to 5 which is true X is 5.
So we end up with false or true which evaluates to be true overall because or only requires one side
to be true.
Lastly we have not and the way that not works it's a little bit different.
It doesn't take a left or right side it just negates or flips the value of what ever we apply it to
.
So if something was true and we put a knot in front of it it then becomes false.
If something was false and we put a knot in front of it it then becomes true.
So in this case the expression X triple equals Y which is false.
Five is not equal to nine.
So the inside is false but there is a bang or a knot in front of it which negates the whole thing and
turns it to be true.
So let's go ahead and give this a shot with a quick exercise.
So below there are three lines of code.
We have a variable x set to turn a variable y equal to the string A and then a short expression here
that is using or in the middle.
So go ahead and evaluate this yourself.
Don't just copy and paste it into the console.
We'll go over a solution.
So just try and predict what the outcome is.
If it is true or false based off of what we've covered so far.
OK.
So this exercise if we plug in 10 for X and the string A for y.
Let's start on the left string a triple equals the string B.
That is not true or x is greater than or equal to 10 which is true because X is 10.
So we have false or true which gives us of course true.
And we could definitely evaluate this over in the console just to verify our work just Center and you'll
see that we get true.
So one more exercise here.
This time it's a little bit longer but it's the same basic components.
So resist temptation just to type it into your cons..
Make sure you plug in three for X and 8 for Y and see if you can figure out if the whole expression
is true or false.
OK.
So hopefully you did that.
Let's go over the answer now.
So as I do this let's start by identifying the overall pattern here.
So we've got a left side and a right side with an and in the middle.
So we need both of these to be true.
So let's start with the left side.
We have X is 3 y is 8.
So this first line X which is three the number double equals three the string that is true because double
equals or.
And this is a small point here.
But technically if the left side of an OR statement is already true that means the entire statement
is true.
Javascript doesn't even care what's over here it's called short circuiting because we only need one
out of two to be true.
If we already get that on the left then we're done.
But just to evaluate this for more practice X triple equals Y is false.
Three is not the same as eight so this whole thing evaluates to true.
However there's also a bang here.
So that negates the whole thing to be false and just like before this actually short circuits the entire
expression because we haven't and here and if the left side is false that means the entire and must
be false because we were looking for two out of two and if we've already missed one and this is false
then it doesn't matter what's over here.
So the whole thing is false.
But just to verify that.
Let's go over to this side.
So we have an and in the middle Why is not equal to 8.
That is false and we haven't and in the middle.
So that short circuits one more time.
This entire thing.
Because if we have the left side is already false then it doesn't matter what the right side is.
But just to double check once again X is less than or equal to Y three is less than or equal to 8.
That is true.
So we end up with false and true which is false.
And then we negate that whole thing.
So that gives us true.
So we have false and true.
So that evaluates to be false.
And just to double check you can paste it into the con..
And if we do that we get false.
So we have one less thing to cover here which is this idea that every value in javascript is inherently
truthy or falsie.
So aside from the obvious bully and true and false which are clearly true or false every other value
has this inherent truthiness or falseness that javascript assigns them.
So I'm going to open up my console and just show you a way of finding this out.
So if I just want to know if the string hello is true or false see if I just type the string and hit
enter it's just going to give me the string back.
So what I can do is actually put it in a boolean statement and the simplest one is just to negate it
.
So if I negate the string hello and I get true back that tells me that the string hello is falsie because
I indicated Likewise if I get false back that tells me that the string hello is truthy and I do get
false which means that the string hello is Trixy.
The other thing that I can do is double negated which will flip it and then flip it back and tell me
that it's truthy.
And likewise I can do the same thing on an empty string and you'll see that it's an empty string is
actually falsie.
Same thing with no
0 not a number.
And when you do negative 1 you'll see that it's actually true.
So these are things that in some languages behave a little bit differently.
So just because in javascript negative one is truthy that does not mean that in other languages it is
automatically Trixy.
So here's a quick recap.
The hard rule is that the values false zero empty string no undefined and not a number are always falsie
and everything else is truthy.
So we have one less exercise here which is just like the others except now it's testing truthiness and
faultiness.
So three strings and empty string string ha ha and the string false.
Evaluate this don't just run the consul we'll go over solution.
Plug in the values mentally and see what you come up with.
It's a whole thing true.
Or is the whole thing false.
OK.
So the answer here is plug in string message and is funny.
So you'll notice that we have an oar and something else.
So let's go ahead and solve this first.
Or on the left.
So we have S T R or string which is an empty string which is falsie.
So false or the string ha ha.
Which is truthy.
So false or true which gives us true for this little section here.
Then we have true and is funny which is the string false which is which is a little tricky but the string
falls could always double check is truthy.
So that tells me that true and true is true.
And the last thing you have to do is negate the entire thing which will give me false.
So just to verify that you can copy this answer and I get false.
Great.
So in the next section we're actually going to see how we make decisions in javascript using these boolean
logic expressions that we've just talked about.
How do we run some code.
If something is false how do we run some other code.
If something is true