Boolean Expressions

A fair amount of computer programming is checking the condition of something and then doing one thing if that condition is right and another thing if that condition is wrong. We call that a Boolean operation. There are two options and one must be chosen.

First, what is a Boolean expression? A Boolean expression is any statement in programming that can be evaluated as either true or false.

JavaScript

// To get the results below open your browsers developer tools, 
// hit F12 on the keyboard. Then select the console and you 
// will be able to type the code below

1 === 1
true

0 === 1
false

There are three logical Boolean operators which are common to most programming languages and are used to build Boolean expressions. They are “and”, “or”, and “not”.

And

Just like in English, “and” is used to put two things together. For an “and” expression both sides must be true or the expression will evaluate as false. Because both sides must be true, “and” expressions will short circuit to false if the first part is false and the second part will not be evaluated at all. For example

JavaScript

// "and" is represented in JavaScript as &&
// the & symbol can be found on the keyboard above the number 7
true && true
true

true && false
false

false && true // In this case evaluation of the expression stops 
              // at the &&, true is never evaluated
false

false && false // In this case evaluation of the expression stops 
               // at the &&, true is never evaluated
false

Or

Next up is “or”, just like “and”, “or” is used to put two things together. Unlike “and”, “or” only requires one side of the evaluation to be true. Because only one side must be true, expressions will short circuit to true if the first part is true and the second part will never be evaluated. For example

JavaScript

// "or" is represented in JavaScript as ||
// the | symbol can be found on the keyboard above the enter key 
// on the backslash (\) key

true || true // In this case the evaluation of the expression stops 
             // at the ||, false is never evaluated
true

true || false // In this case the evaluation of the expression 
              // stops at the ||, false is never evaluated
true

false || true
true

false || false
false

Not

The last Boolean operator that we are going to discuss is “not”. Unlike the other two operators, “not” is not used to combine two other expressions. Instead, “not” will negate, or invert, the expressions value. For instance, applying “not” to a true expressions will make that expression false and visa versa.

JavaScript

// "not" is represented in JavaScript as !
// the ! symbol can be found on the keyboard above the number 1

!true
false

!false
true

Parenthesis

Finally, do you remember the order of operations we discussed in the Basic Math Operators post? We can use parenthesis with Boolean expressions as well. They have the same ability to control what gets evaluated when for Boolean expressions as they do for mathematical expressions.

JavaScript

false || !false && false
false

false || !(false && false)
true

true && !true && false
false

true && !(true && false)
true

We’re Done

That is all there is to Boolean expressions. As Always, I hope you learned something from this, but if not please look at some of the other articles as they are more advanced and may lend you some insight in the world of software development.

Leave a Reply

Your email address will not be published. Required fields are marked *