Page of Alena's post with the title Boolean constants, De Morgan's Laws

⭐️ 💖

Back to home page

Alena Nikolaeva

Germany

Frontend Developer

Boolean constants, De Morgan's Laws

Updated: 19.08.2020

15 min

Boolean Logic in JavaScript

This post has been inspired by this quote from Nick Gard.

“JavaScript is loosely typed, it coerces values into booleans in logical expressions. if statements, &&, ||, and ternary conditions all coerce values into booleans.

What is boolean logic and where does it come from? How does Javascript evaluate Boolean statements?

Introduction

Boolean constants and variables, De Morgan's Laws

Boolean algebra differs in a major way from ordinary algebra because Boolean constants and variables are allowed to have only two possible values, 0 or 1. A Boolean variable is a quantity that may, at different times, be equal to either 0 or 1. Thus, Boolean 0 and 1 do not represent actual numbers but instead represent the state of a voltage variable, or what is called its logic level. Boolean algebra means for expressing the relationship between a logic circuit’s inputs and outputs.

In Boolean algebra, there are no fractions, decimals, negative numbers, square roots, cube roots, logarithms, imaginary numbers, and so on. In fact, in Boolean algebra there are only three basic operations: OR, AND, and NOT.

In human language, a silly example can be "If I am hungry then eat some food, otherwise if I am thirsty, drink some water, otherwise, take a nap."

In programming you can combine some operations and get this simple program:

if (my_coffee_is_ ready && toast) {
  have_breakfast()
} else { 
  ...
}

If I have my coffee ready AND I have my toast the I'll have my breakfast. BOTH conditions have to evaluate to true (have to be true) before the entire expression is true.

The primary way to combine two boolean expressions into one is through the use of AND or OR. In most programming languages, AND is written using double ampersands: &&. OR is written using double pipes: ||

It's also useful to list some of the De Morgan’s Laws, in short those are sets of rules or laws developed from the Boolean expressions for AND, OR and NOT using two input variables, A and B.

DeMorgan's Laws tell us how to translate from a "positive" version of a statement into a negative version, or more concisely: The DeMorgan laws tells us how to translate an logical expression (E) into NOT(E).

For instance, NOT(🍌OR 🍏) is equivalent of NOT(🍌) and NOT(🍏) NOT(🍌OR 🍏) is equivalent of NOT(🍌) or NOT(🍏). Another law is double negation🍌 === !!🍌, which is dangerous thing to do in Javascript, since out banana is coerced into boolean. There are others DeMorgan's Laws which you can find here

The practical example of transforming logic with De Morgan's Laws:

if (my_coffee_is_machine > 1 cup) {
  turns_off()
}          

Which is the same as

while (! (my_coffee_is_machine > 1 cup)) {
  continue_making_coffee()
}          

You can expand this silly example by adding an additional condition and you get the logic of banana and apples above.

Remember the famous infinite loops in JavaScript and how fun it is to make one?

// Initiate an infinite loop
while (true && NOT(condition)) {
    // execute code forever
}         

I have found this article by Harvard University about infinite loops and why they happen and knowing about boolean algebra operations, some of these examples are very useful.

Going back to JavaScript, you need to remember AND, OR and NOT as &&, ||, !. Logical operators are typically used with boolean (logical) values, and when they are, they return a boolean value. Boolean logic also contains the XOR operator, but JavaScript doesn't support it.

NOT

  • Check if something exists or not
if (!x) {
    do something
}       

(careful with falsy values here: null; NaN; 0; empty string ("" or '' or ``); undefined)

  • Double !! (NOT NOT)
!!coffee_ready

is equal to

coffee_ready 
!!true === true // This is equal
!!false === false // This is equal
  • Note that !! also converts Object to boolean. !!expression returns a Boolean value. You should use it when you want the ensure the return type of a function is a boolean.
  • NOT and OR
const a = 3;
const b = -2;

console.log(!(a > 0 || b > 0));
// expected output: false
     

Conclusion

I hope it's useful to get a quick overview of Boolean expression and operations before using that logic in a wild. Personally, it was my favorite topic of Discrete Logic and Mathematics. Hope you find it interesting too. I'm sure there is a lot to cover here, and part 2 is yet to come.

P.S. Why Boolean logic?

George Boole is these days credited as being the "forefather of the information age". An Englishman by birth, in 1849 he became the first professor of mathematics in Ireland’s new Queen’s College (now University College) Cork.

  • Credits - I'm quoting Donald J. Tocci book "Digital Systems" 20th edition, it's physical book that I have bought, so there is no link. I also recovered a lot of my notes from articles and used some examples from blogs.
  • Idiomatic Javascript by Cory Finlinson
  • Conditional logic in JavaScript by Nick Gard