Boolean Logic
Boolean logic is a branch of computer science that uses true and false values to solve complex logical problems. To do that, it uses several logical operators to combine true and false values, much like we use arithmetic operators to combine numeric values. We aren't going to go deep into it but only talk about the most basic logical operators, which are the AND, OR and NOT operators.
You have to, however, note that boolean logic is not specific to JavaScript. This is all true for all programming languages. Let's now understand how these operators work and use an example to illustrate it. Consider a user named Jacob with the following suppositions :
X : Jacob is a Software Engineer
Y : Jacob lives in Paris
Suppose X
and Y
are two variables that can either be true or false. Meaning Jacob might not be a Software Engineer and might not live in Paris, and on the other hand, he could be a Software Engineer and live in Paris.
Using the AND
operator like X AND Y
, we can combine these two boolean variables like this :
Jacob is a Software Engineer AND lives in Paris
Now to know the result of this operation, we use a so-called truth table which looks like this :
AND | TRUE | FALSE |
---|---|---|
TRUE | TRUE | FALSE |
FALSE | FALSE | FALSE |
Here we have two possible values for each of the values X
and Y
, which gives us four possible combinations of results. All we have to do then is to plug in the values of our variables and get the result of the AND
operation from this table.
What we can see from the table is that only if X
( Horizontal ) and Y
( Vertical ) are true, the result of the operation will be true as well. In short, the AND
operator returns true if both X
and Y
are true, and in all other situations, if either X
or Y
is false, then X AND Y
will also be false.
The next operator that we are going to see is the OR
operator. It kind of works in the opposite way. With our previous example from the beginning of this lecture, we could use the OR
operator to determine whether Jacob is a Software Engineer or lives in Paris
. By just thinking about this, we can already intuitively imagine that the OR
operator will be true if just one of the variables is true.
A look at the truth table confirms exactly that :
OR | TRUE | FALSE |
---|---|---|
TRUE | TRUE | TRUE |
FALSE | TRUE | FALSE |
Unlike the AND
operator and the OR
operator, even if one of the variables is false, the outcome will still be true. In other words, if either X
or Y
is true, then the OR
operator becomes true.
The last operator that we are going to see is the NOT
operator represented by an exclamation mark ( !
). This operator is a lot simpler because it doesn't combine multiple values. Instead, the NOT
operator acts on only one boolean value and inverts it; .i.e., if X
is true, it will become false, and if it is false, !A
will become true.
Let's have some practical examples :
apples = 60
X : Apples are greater than or equal to 100
Y : Apples are less than 80
Let's consider a variable apples
with a value of 60
and two boolean variables X
( Apples are greater than or equal to 100 ) and Y
( Apples are less than 80 ). Let's determine the value of each of them.
Having apples
with a value of 60
, then variable X
will be false, and Y
will be true. This is the baseline. Now let's try to combine this variables X
and Y
using some logical operators.
apples | 60 |
X : Apples are greater than or equal to 100 | FALSE |
Y : Apples are less than 80 | TRUE |
The first exercise is the NOT
operator; that is !X
. We know right now that X
is false
, so the result of !X
as we just learned, is true
because all it does is to invert the logical value of the variable.
The next one we have is X AND Y
. We already know that X
is false
and Y
is true
. So we have basically false AND true
. From a look back to our truth table, we can easily determine that the result of this must be false
.
The next is X OR Y
. From the truth table, we can see that we get true
as the result; showing that it is enough for one variable to be true for the whole expression to be true as well.
Let's now check !X AND Y
. We already know that !X
is true
and Y
is true
. This time we are combining multiple operators, and we already know that true AND true
will, of course, be true
as well.
Another one that we can check is X OR !Y
. As a side note, the NOT(!)
operator has proceedings over the OR
and AND
operators. Basically, the values are inverted first, and only then, they are combined using OR
or AND
.
We know that X
is false
and Y
is true
, so !Y
must be false
. Thus, false OR false
is also false
. This is the only way in which the OR
operator can be false.
!X | TRUE |
X AND Y | FALSE |
X OR Y | TRUE |
!X AND Y | TRUE |
X OR !Y | FALSE |
I hope all this made sense to you. In the next lecture on logical operators, we will use these operators in some, code and by then, it will make more sense to you. We will probably won't even need the truth tables anymore because it will become quite intuitive how both of these operators actually work.