Boolean expressions have tripped up many students when they first learned how to code, especially those complex Boolean expressions with too many parentheses. It stumped me as well (my grade went down because of it ๐ข). Once you get the hang of it though, it becomes a piece of cake! Lets look at it right now.
Logical Operators
These are basically just operators that can be used to create more complex Boolean expressions whenever you need one.
&& -> AND
|| -> OR
! -> NOT
If there are 2 Boolean variables a
and b
, a && b
will only evaluate to true if both a
and b
are true. a || b
will only evaluate to true if either a
or b
is true. !a
evaluates to the opposite value of what a
currently is (true --> false, false --> true).
When using && or || in Boolean expressions, you can figure out what the expressions evaluates to by looking at the first half of the expression (the first Boolean operand). This is called short circuited evaluation.
If you are talking about && (and), short circuiting occurs when the first half of the operand is false. The entire expressions is going to be false, so there would be no point in evaluating the second half. If you are talking about || (or), short circuiting occurs when the first half of the operand is true. The entire expression is going to be true regardless of what the second half evaluates to.
Example
What would be the output of the following code?
Scroll down below the code segment to see the answer
function testBooleanExpressions() {
const temp = 95;
const isRaining = true;
const trafficJam = false;
if (temp > 80 && !isRaining) console.log("Enjoy the weather!");
else if (isRaining || trafficJam) console.log("Drive carefully!");
else console.log("Have a nice day!");
}
testBooleanExpressions();
The code would print out "Drive carefully!". If you check the first expression, temp (which is 95) is greater than 80, but !isRaining
evaluates to false because it's currently true; therefore, we would move on to condition 2. In condition 2, isRaining
is true, so it short circuits and automatically prints out "Drive carefully!".
De Morgan's Laws
Augustus De Morgan was a 19th century mathematician that proved the following laws (which we'll take a closer look at). If we have 2 Boolean variables a
and b
:
Law 1: !(a && b) == (!a || !b)
Law 2: !(a || b) == (!a && !b)
Law 1 basically says if you distribute the ! (not) to the a
and b
, flip the && (and) to an || (or). Law 2 basically says the same thing except the && and || switched places.
Distributing the ! (not) with a Boolean expression flips the relational operator to its opposite.
!(x > 0) == (x <= 0)
!(x <= 0) == (x > 0)
...
For example, if we have the expression (x < -5 || x > 10)
, what would be the opposite of it? Well, our new expression would be !(x < -5 || x > 10)
. If we distribute the "not", we can use De Morgan's second law to get (!(x < -5) && !(x > 10))
. If we distribute the "not" again, we can just flip the relational operator and get: (x >= -5 && x <= 10)
.
Conclusion
Learning how to use logical operators and De Morgan's laws takes time and practice. I learned that the hard way, but once you know how to use them, they are a breeze to identify and use in your code. I hope you found this article useful, and please share it with anyone that may need it.
Signing off ๐