In the last post, we took a look at Strings, Nulls, Undefined and NaNs in JavaScript. Today, we will take a look at Booleans, their operators, and their logic. A Boolean is yet another JavaScript data type and they relate back to the other data types that we have covered thus far in this tutorial on JavaScript.

The strict definition of a boolean is a value that has a binary set of values; in this case, the value can be either true or false. Booleans are an extremely important part of the imperative style of programming because they control most of the flow of the program.  Various operators and operands can be used to determine in which way a program should go and booleans are the most common type of data used for this particular process.

Breaking Down Computation

If you know anything about computers then you have probably heard about how every piece of data ultimately breaks down to a series of ones and zeroes; this is what is called binary data. Binary data is also technically boolean because there are only two possible values that a binary piece of data can take. In fact, in older languages like early common lisp, binary data was used to denote booleans rather than explicit keywords like what we have in JavaScript and many of the languages that are in use today.

In JavaScript, our Boolean values are the keywords, true and false. To really talk about Boolean values, we should also talk about conditionals.  Conditionals are logical sentences in the programming that evaluate to either true or false. In JavaScript, conditionals follow a format like the block below.

Notice the use of the parenthesis for the conditions and the curly brackets for the code body, also, in the actual code the “then” is implied.

boolean-falseIn JavaScript, certain values and types are considered “falsy”: 0, “”, NaN, null and undefined are all “falsy” values. This means that if we put one of these values as the condition of a conditional statement we will always get a failing statement. It is as almost as if we were inputting the keyword false as the condition. Out of these values, only the empty string "" and 0 will return true if they are set equal to the keyword false. 0 == false and "" == false both return true as though they were both the false keyword. Try inputting various combinations of these different falsy values in conditionals to see what happens.

A real example would look more like this example.

If you open your JavaScript console and type in the code above, you will get the return value of false. You can also chain these conditionals together endlessly (though there are usually better ways with longer conditionals).

Make sure that you always keep a space between your else and ifs. In this case, this statement will return true.

The Basic Operators of Booleans

When writing the condition part of the conditionals, you can make use of eight different operators: greater than >, greater than or equal to >=, less than <, less than or equal to <=, equal to ==, not equal to !=, and the two special operators, equal in type and value === and not equal in type or value !==. The last two operators === and !== are unique to JavaScript because of the dynamic typing in the language and in most cases you will use these over the normal == and != when writing conditions and conditionals.

decision_makingLet us look back at the second example, more specifically the ("54" === "54") condition. If we were to change one of the "54" from a string to a number, the condition will now fail if we keep using ===, however, if we use (==) then the condition will pass because it is only checking the value and not the type as well.
You may also remove the else portion of a conditional statement if you don’t need it, say you only need a return value if the statement passes. You would just write something like the examples below.

If that condition does not pass then the code inside of the brackets will not be run. If you were to change the condition to a condition that passes such as 9 <= 9 then the "it's true" will be returned in the console, otherwise you will just receive a return value of undefined. Go ahead and try it with various different conditions until you understand how the different operators work.


In today’s post, we talked about the basic operators of booleans and conditionals. We introduced control flow through the use of if and if…else conditionals. In the next post, we will take a look at variables. As always, if you have any questions please feel free to leave them in the comment section below.