Instructor: Let's now return to our code
and learn how logical operators work in JavaScript.
And let's actually use the Boolean variables
from the last lecture.
So the one about having a drivers license
and having good vision.
So, we are gonna create a variable for each of them.
And the first one will be has drivers license
and let's start by setting it to true.
And remember that this one was variable A.
And then let's create has good vision
and also, let's initialize it with true.
So these were the two A and B variables
that we used in the last lecture as an example.
And so it's a good idea to use these here as well.
Okay, but now let's get started with the end
and or operators.
So let's log to the console, of using the end operator
on these two variables.
So has drivers license
and now the end operator in JavaScript is this one.
So it is we can combine two logical values.
So has drivers license and has good vision.
So let's see what the result will be.
So this one is true, right?
And this one is true as well.
So the result of true and true, should be true, right?
So let's see and yes, so that works.
But now, of course, we will change one of them to false.
So remember, the result of true and false will be false,
because it's enough for one variable to be false
for the result of the operation to be false.
And so here, we can now confirm that
and indeed, that is the result.
But now let's see what it would look like
with the OR operator.
So I just copy this and paste it here.
Now, instead of end we use OR and that looks like this.
So again, true or false.
What do you think it will be?
Remember from the last lecture and the result is true.
So it's enough for one of the variables to be true
for the whole operation to become true.
So I hope that's nothing new at this point.
And of course, finally we can also use the NOT operator
to basically invert one of the values.
And the NOT operator in JavaScript is the exclamation mark.
So not has driver's license will become,
so it will change from true to false.
All right, and now since we know how all of this works,
we can use these variables to take a decision.
So let's say that we want to determine
whether Sarah should drive or not.
So let's create a new Boolean variable and use the others
to determine that.
So let's say should drive
and our model is that Sarah should drive,
if she has a driver's license
and at the same time has good vision, okay?
And so as I mentioned in the last lecture,
we can now use these Boolean variables,
and this Boolean logic to model complex situations.
And this one is actually pretty simple,
but we will take it up a notch by the end of this lecture.
So remember, model is that she has a driver's license,
and that she has good vision.
Okay, and now we can use this variable
to finally take the decision.
So let's then use this Boolean variable here and say,
if she should drive, then lock to the console.
Sarah is able to drive
and if not,
just log someone else
should derive.
Now, okay, and actually we can put the condition right here
in the IF condition.
So this was just to show you a little bit
the thought process.
But as I mentioned earlier,
we usually create the condition right here
in the IF statement.
So give it a safe and now let's run it and the result is,
someone else should drive.
So that is because has good vision is false,
and so our condition of has driver's license
and has good vision is not true.
Because it's only true in case that both of them are true,
as you already know by now.
So this one is false right now.
And so therefore the else block will be executed.
But of course, we can change it once more.
And so right now, both of them are true.
So has driver's license true and has good vision true.
So true and true, is true and so Sarah is able to drive.
Let's check that and indeed, great.
And but this one working now perfectly,
let's take it to the next level
and add another Boolean variable here.
So, let's now also add is tired.
And let's set it to true as well.
So we can say that this is our variable C.
So to use more than two variables with, for example,
the end operator, let's just go ahead and copy this one.
So if you want to say has driver's license
or has good vision or is tired.
All you have to do is to add another OR operator here,
just like you would add another plus
or another minus operator.
And the result of this should of course, be true,
because while all of them are true.
And indeed, it is.
And if we set them all to end,
then it is also true, because all of them are true.
And if we set this one to false,
then as you can guess the result will be false.
And that is because as I explained in the last lecture,
it's enough for one of the operands to be false
to make the whole end operation false as well.
Okay, but let it set back to true.
And now, we can use this to improve our decision making,
whether Sarah should drive or not.
So with this third variable now,
we want Sarah to be able to drive
if she has a driver's license, if she has good vision
and if she is not tired, right?
So let's now translate that into a condition.
And I will just copy this one
and then comment all of it out using Command
or Control/ just like many times before
and now we will improve this one.
So this one, of course, is the same
that she needs to have a driver's license and good vision.
But now additionally, we want her to be not tired.
So right now she is tired, it's true
and that's, of course, not good for driving.
So we want the opposite of being tired.
And so therefore, we need to invert it.
So do you remember how that works?
All we need to do is to use the NOT operator.
So not is tired, all right?
So let's read this again,
Sarah is able to drive if she has a driver's license.
So if this one is true, if she has good vision,
so if this one is true and if she is not tired,
so if all of this is true here.
So only in this condition, Sarah is able to drive.
Now how is this part here true?
Well, it's true, when is tired is false, right?
So what do you think will happen
in this situation right now?
Well, let's test and then analyze.
So we get the result, someone else should drive.
So why is that?
Well, this one is true,
this one is true as we see up here,
and then is tired is also true.
However, not is tired is false.
So this part here is false.
And so true and true and false is of course, false.
And so therefore, we get the else block,
someone else should drive.
Now to fix this,
we need to change is tired to false, all right?
And so right now is tired is false
and therefore not is tired will become true.
And then we have true and true and true
and then in this situation, Sarah will be able to drive.
Yes, indeed, it works now.
So as you see, with these three Boolean operators,
we can model all kinds of companies decisions like this one.
Now, I know that this was probably a bit confusing,
but please don't worry too much,
because it's all very confusing for everyone
when they first learn how to program, okay?
Just take some time after this video
and analyze again exactly what happens here.
Maybe also together with the last video
to really make sure that you understand
what is going on here.
But of course, the best way to retain any knowledge
is by practicing and reinforcing it.
And so let's do that now in the next coding challenge.