Boolean logic

See section 5G of the book.

This page has been translated into French.

Boolean logic is something used in most programming languages, including JavaScript. It's very useful to know at least a little bit about it. In JavaScript, you mostly need it in if() statements.

This is only a quick and dirty explanation written for new programmers. In addition, it wholly centers on JavaScript uses of Boolean logic.

I first discuss the basic theory, then introduce AND, OR and NOT in JavaScript. Then I present a Boolean generator with which you can make your own Boolean statements and see how they evaluate.
After that I explain when JavaScript makes a variable false and finally how to see if an object exists. When you've understood all this you can work with Boolean logic in JavaScript.

The basic theory

In Boolean logic, a statement can have two values, true or false. When using Boolean logic in philosophy, the statement can be a sentence, like

It rains today.

In more down-to-earth applications like JavaScript, a statement is something like

x == 4

Both statements can be either true or false. When writing a program it is often necessary to see if a statement is true or false. Usually this is done by an if() statement. If the statement x == 4 is true, then do something:

if (x==4) {
	do something
}

All this is not surprising. Boolean logic, however, also offers possibilities to evaluate a whole string of statements and see whether the whole string is true or false. Like:

It rains today AND my feet are getting wet

In Boolean logic, this longer statement is true if it rains today is true AND my feet are getting wet is true.

It rains today OR my feet are getting wet

In Boolean logic, this statement is true if it rains today is true OR if your feet are getting wet is true OR if both statements are true.

This is also very useful when writing programs. For instance, suppose you want to do something if x==4 OR y==1. Then you write:

if (x==4 || y==1) {
	do something
}

The statement (x==4 || y==1) is true when x is 4 OR y is 1.

AND, OR and NOT

For JavaScript purposes, you need to know AND, OR and NOT:

Boolean logic also contains the XOR operator, which is true when exactly one statement is true (but not both). JavaScript doesn't support logical XOR.

When you use only two statements, this is all easy. It gets more complicated if you want to use three or more statements, like:

if (x==4 && (!(y==1) || z==0) {
	do something
}

As in mathematics, the bit that's between the brackets () is evaluated first. So this example is true and the code is executed if x is 4 AND (y is NOT 1 OR z is 0 OR both).

Try it

At first sight this seems hideously complicated. I could go on writing more about how Boolean logic works, but it's better to try it for yourself.

In the table below you see three statements, X, Y and Z. All of them can be either true or false. Fill in the AND/OR, the NOT's, where the brackets are and the value of X, Y and Z, then hit Boole() to see how your statement evaluates.

X . Y
(X . Y) . Z
X . (Y . Z)
not
X
true
false
and
or
not
Y
true
false
and
or
not
Z
true
false

Play around with it until you start to understand what it's all about. Later you might try to make a prediction before hitting Boole().

Checking a variable

Above we have seen that we can use Boolean logic in a statement like

if (x==4 && (!(y==1) || z==0) {
	do something
}

A second way to use Boolean logic is to see if something exists or not. For instance

if (!x) {
	do something
}

In this example the code is executed if x does NOT exist (x=false).
x is false

In all other cases x is true and the code is not executed. Note that you must define the variable x somewhere or the browser will complain.

Support detection

Finally, a statement is false if it searches for a JavaScript property, array or method that does not exist in the browser. For instance, to know if the method document.getSelection() exists, I do

if (document.getSelection) {
	do something with document.getSelection()
}

If the method doesn't exist the script that uses the method is not executed. This is a fail safe method of avoiding JavaScript errors.

For more information about this important feature of JavaScript, see the Object detection page.