When we learn about if-else statements,
comparison and equality operators
and Boolean values.
We also need to learn about logic.
And in particular about Boolean logic.
So let's do that in this video.
So basically Boolean logic
is a branch of computer science,
which uses true and false values
to solve complex logical problems.
And in order to do that,
it uses several logical operators
to combine true and false values.
So much like we use arithmetic operators,
to combine numeric values.
Now we're only going to scratch the surface here
and talk about the most basic logical operators,
which are the AND, OR, and NOT operators.
And note that Boolean logic
is actually not specific to JavaScript.
This is all true for all programming.
But anyway,
let's now understand how these operators work
and use an example to illustrate it.
So here we have two Boolean variables
that can either be true or false.
So A is that Sarah has a driver's license
and B is that Sarah has good vision.
So both of these are of course conditions,
that can be true or false
because Sarah could have no driver's license
and she could have no good vision,
right.
And of course on the other hand,
she could also have a driver's license
and could have good vision.
Now using the AND operator like A aND B,
we can combine these two Boolean variables
like Sarah has a driver's license and good vision.
Okay.
But now what will be the result of this operation?
Well for that,
we use a so-called truth table,
which looks something like this.
Here we have to two possible values
for each of the variables,
A AND B,
which gives us four possible combinations of results.
And so then all we have to do
is to plug in the value of our variables,
and to get a result of the AND operation
from this table.
So what we can see from this table
is that only if both A and B are true,
the result of the operation will be true as well.
And that makes sense, right!
After all,
it's called the AND operator.
And so again,
what the AND operator does
is to return true
only if both A AND B are true.
In all other situations,
if either A or B are false,
then A AND B will also be false.
And we can also generalize this to more operands.
So to more than just two values,
we could do A AND B AND C,
and results would be true
only if all of them are true.
And if one of them is false,
then the result is immediately false as well.
Now I know that all of this,
might look a little bit confusing,
but don't worry We will have an example,
a little bit later in this lecture,
and we will also use this in practice
and the next one.
And after some practice,
you will internalize all of this
and you won't even need any truth tables.
Now moving on,
on the other hand,
we also have the OR operator,
which kind of works in the opposite way.
So with our current example,
we could use the OR operator
to determine whether Sarah
has a driver's license or good vision.
And just by thinking about this,
we can already intuitively imagine
that the OR operator will be true
if just one of the variables is true.
And in fact,
a look at the truth table confirms exactly that.
So unlike the AND operator and the OR operator,
even if one of the variables is false,
the outcome will still be true.
Or in other words,
if either A OR B is true,
then the OR operation becomes true as well.
And that's why it's called the OR operator.
And once more,
if we have multiple variables,
it's enough for one of them to be true,
to make the whole operation become true as well.
Okay, I hope this makes sense.
And once again,
I can assure you
that it will make sense
once we start using this in practice.
Anyway,
we finally also have the NOT operator,
which is a lot simpler
because it doesn't combine multiple values.
Instead,
the NOT operator acts on only one Boolean value
and it basically just inverts it.
So if A is true,
it will become false.
And if it's false,
then NOT A will become true,
simple as that.
And that's basically
how these three most important logical operators work.
But of course,
the best way to understand it
is to use a more practical example.
So let's go again back
to using the age,
and this time we set it to 16.
And then we have,
our two Boolean variables.
The first one is age is greater or equal 20.
And the second one is age is less than 30.
So let's determine the value of each of them.
So if age is 16,
then age is greater or equal to any
must be false.
Right!
And of course it is,
and age is less than 30 is true
because 16 obviously is less than 30.
Okay.
So that's the baseline basically,
and now we will start to combine
these variables A and B
using some logical operators.
So the first exercise
is the NOT operator.
So NOT A,
and we know right now that A is false, right?
So the result of NOT A,
as we just learned is true,
because all it does
is to invert the logical value of the variable.
Next one we have A AND B,
now we already know that A is false and B is true.
So I'm just putting these values here again,
to make it a bit easier to follow.
Okay.
So we have basically false and true.
So from a look at our truth table here
at the right side,
we can easily determine
that the result of this must be false.
And so that's exactly
what we learned before.
So when we have the AND operator,
it's enough for one of the operands to be false,
to make the result of the operation also false.
Now we have A OR B,
and so let's check out the truth table in this case.
And so in this case,
we actually get true.
And so this shows that it is enough
for one variable to be true
for the whole expression to be true as well.
Next up let's check NOT A AND B.
And we already know
that NOT A is true and B is true.
And so at this time for the first time,
we are combining multiple operators here
and yeah we already know that true and true
will of course be true as well.
And now another one combining two operators,
so A OR NOT B.
And just as a side note,
the NOT operator actually has proceedings
over the OR and AND operators.
So basically the values are inverted first
and only then they are combined using AND or OR.
So we know that A is false and B is true.
So NOT B must be false, right
then false OR false is also false.
And actually that's the only way in which
the OR operator can be false.
If all of them are false basically.
So I hope that made sense to you.
And so in the next lecture,
we will actually use these operators in a code.
And then it will make even more sense to you.
So by then,
we probably won't even need the truth tables anymore,
because it will become quite intuitive
how both of these operators actually work.
But anyway, let's now move on
and I see you in the next video.