Instructor: Let's now take conditional to the next level
and learn about Boolean logic.
And, in this small example, we want to log to the console,
depending on Johns age, if he is a boy, a teenager,
a young man, or a man, okay.
And, so, let's start, again, by defining the first name
as John, and then the age.
And let's start here with 16, for example.
Alright, so we use our if else statement.
And we can say that if John is younger than 13,
well, then, he is a boy, alright.
So console.log firstName + is a boy, alright.
And then we can say else,
and let's just copy this line of code here,
is a man.
Okay, so that covers the boy and the man.
But how do we actually decide if he is a teenager
or a young man?
We need some more options here, right?
And we can actually do that with JavaScript.
So instead of just having the if and else block,
we can also have an else if block.
Okay, so let me show that to you.
So here we can have else, and then a new if.
So let's put it back here.
And, so, here we can put some more text.
And, actually, we need to add a new condition here.
And so what do we want to have here?
So let's first put in the result here, actually,
so that we know what we're actually talking about here.
So let's say that he is a teenager.
And, so, teenager basically means
that he's between 13 and 20, right?
So let's write that down here as a comment.
Between 13 and 20.
Well, how do we write a logical condition
to express exactly this?
So how do we say that the age should be between 13 and 20?
Well, that's kind of difficult, right?
So let's think about it a little bit more.
So between 13 and 20, what this really means
is that the age is greater or equal, 13, right?
And, at the same time, it's also less than 20.
Right, so, again, having an age between 13 and 20
should mean that the age is equal or greater than 13,
and less than 20.
So these two conditions here at the same time, okay.
And so what we need is an operator for this.
And, so, there actually, in JavaScript, is an end operator.
And in order to learn how that end operator works,
we need to talk about Boolean logic.
So, in a very broad sense,
Boolean logic is a branch of computer science
that deals with true and false values.
And it's actually something quite important in coding.
And so you should make sure to really understand this part.
Alright?
Now there are several logical operators
that deal with true and false values.
But the most important ones
are the NOT, AND, and OR operators.
So let's start by looking at the AND operator.
And here we have a table called a truth table,
which allows us to see the outcome of the operator.
So the AND operator works like this.
Suppose that we have two variables, A and B.
Only if variable A and variable B are both true,
the expression A and B will become true, okay.
In all other cases, the result will be false.
And that's why it's called the AND operator.
Because A and B must both be true
in order for the result to be true as well.
So if either A or B is false,
well, then A and B will become false as well.
And the same applies if there are more than two variables.
Only if all the variables are true,
then the AND expression will become true as well.
Alright, and now the second truth table
is for the OR operator.
And here things work a bit differently.
So, with the OR operator,
it's enough for one of the variables to be true
in order to make A or B come true.
And, again, that makes sense, right?
So it's called the OR operator
because only A or B needs to be true
in order for the final expression to be true.
The result will only be false if both variables are false.
And we can see that here in the truth table, right?
And, once more,
it works the same way with multiple variables.
So it's enough for one variable to be true
to make the final result be true as well,
if there are like 3, 4, 5, or even ten Boolean variables.
So that's how the AND and true operator works.
And then, finally, there's also the NOT operator.
And this one is really simple.
All it does is to invert
the true or false value of a variable.
So if variable A is true, then NOT A is false.
And if A is false, well, then NOT A is true.
So, easy, right?
Let's now look at a couple of examples.
So we start by defining a variable called age.
Then we have age greater or equal than 20,
which is, of course, false, because 16 is less than 20.
Then we have age less than 30, which, of course, is true.
And, finally, we have the NOT operator.
So we know that age less than 30 is true.
And we know that the NOT operator inverts the value.
So NOT age less than 30 is, of course, false.
Makes sense, right?
Okay, so the next expression.
We already know that the first part,
age greater or equal than 20, is false.
And we also know that age less than 30 is true.
So, here, we have false and true.
So can you guess the result?
That's right, it's false, because they're not both true.
Okay?
Now the next expression is similar.
But we have the OR operator here.
So it's now true or false.
So what will the result be this time?
Alright, now it's true.
Because, with the OR operator,
it's enough for one of them to be true.
So I hope that made sense.
And so we can now go back
and use what we just learned in code.
Okay, and now that we know about the AND and OR operators,
we actually have a way of writing out the expression
that we need here.
So what we want here is the age
to be greater or equal to 13.
And, that's with the AND operator,
as you saw before, we write like this.
And so we want age greater or equal to 13.
And, at the same time, the age to be less than 20.
Okay, and so that's all we need.
That is our expression here.
Or, basically, our testing condition.
So let's say that the age is 16, so as it is right now.
So if we now load this,
well, then we log to the console, John is a teenager.
And so it's actually working.
So, right now, age is 16.
And so age is, in fact, greater than 13.
So this part here is true.
Then the age is also less than 20, so this is also true.
And so true and true makes true, as we just learned.
And so then this block here is executed.
Now just quickly talking about
the precedence of this operator here,
while it has to be lower than the precedence
of this ones here, right?
Because, of course, we first want this to be executed
in order to end up with the true or false value.
And then this one here.
And, only after that, we want to combine
these two false values using this operator here.
And so, of course, the AND and OR operator,
they must have a lower precedence
than these comparison operators, right?
So let's check if that's actually true.
And, indeed, it is.
So the Logical AND and OR,
they have a lower precedence of 6 and 5
than these comparison, like Less,
and Less Than Equal, Greater,
and, oh, yeah, all these comparison operators here.
So they have a higher precedence
and that's why it works the way it works here, okay.
And now just a final step.
Because we want a boy, teenager,
and we also want like a young man between 20 and 30, okay.
So let's create a new else if here.
Here are the condition, and open a new block.
So a young man.
And what do we want the age to be now?
Well, we want it to be greater or equal than 20.
And, at the same time, it will be less than 30, okay.
And if we didn't put this equals sign here,
well, then the 20 wouldn't be covered anywhere, right.
So let's take this out here.
And go to 20, so set the age to 20.
And let's see what happens then.
So now it says John is a man.
Okay, and that's because this 20 here is not included,
neither in this condition nor in this one.
So here it's just less than 20,
and here it's greater than 20.
But nowhere is the equals sign.
And so that's why we have to include it right here, okay.
We could also have included it here.
But while someone who is 20 years old
is not really a teenager anymore,
and so we include this equals sign up here.
And if we reload it now,
well, then it's gonna say young man.
Okay, and so that worked just find.
Alright, and I think that is enough for this lecture.
So you learnt all about Boolean logic
and the AND, OR, and NOT operators.
And, also, that we can add these else if blocks here
to our if else statements, okay.
So we can have multiple, let's say, if else statements
all in one construct, like this.
Okay, so make sure that you understand all of this.
If necessary, you can maybe review parts of the lecture,
and then let's just move on to the next video.