True or false. Understanding and implementing Booleans

In programming, like in life, it’s sometimes vital to know an answer to a simple question, is it truth or lie. For instance, when a husband asks his wife whether there is $10,000 on their bank account, depending on the wife’s answer (let’s assume they're honest with each other), they’ll decide whether they will spend this summer in Rome or not. If she says yes, he goes and buys tickets to Italy, otherwise, they stay at home and watch Italian movies. There is nothing wrong if we extrapolate this life situation into programming, thus we can use Boolean data types like true and false.

Booleans, unlike objects, are primitive data types, which means they have only one value (objects are not primitive as they have key-value pairs).

Despite having different syntax in different programming languages, Booleans answer to the same question. Depending on the answers, we choose different ramifications, which will lead to different scenarios. It is also important to underline, that Booleans work hand in hand with conditionals (if else statements).

In the example above, if we try to convert it to more humanly language, it says: if the first condition will run only when it is true (amount ≥ 10000), do what is stated in the scope (within curly braces). If that condition is false (or else), do what is stated in the scope of else. This shows that Booleans are explicitly attached to if-else statements.

Sometimes it is handy to convert different values into their true or false values. Although different programming languages can have different values, we’ll stick to JavaScript, and not because this language is better to learn Booleans, but because it’s more feasible to check. All you need to do is to open your browser's console (Cmd + Option + J on a Mac or Cmd +Shift +J on Windows). From there, you can receive some valuable and sometimes unpredictable information as you juxtapose the values.

As you may have noticed, in some cases we use double equal operator (or abstract comparison operator), when in other examples triple equal was used (strict comparison operator). Although it may be a topic for another article, but to quickly differentiate, double equal compares values whereas triple equal compares both values and type. As programmers, we should use the triple equal operator as much as possible, while its comparison is more strict and help avoid errors in the future and makes code more solid.

You can always dive into the console and compare values until you get hang of it while understanding that this “truthiness” and “falsiness” can be essential. Another way to get most of Booleans is by using and (&&) and or (||) operators, which can be tricky at a first glance if don’t dive deep enough

In the first example, with the use of or (||) operator, the first truthy operand is returned (“hello” string). In the second example, if all the operands are false, the value of the last false operand is returned (null). In the third example, where and (&&) operator is used, the first falsy operand is returned (false, the third operand). The last example checks (from left to right) all the operands and, if all of them are true, it returns the last one (“goodbye” string). This last example can be a great short and logical way to compare things and make decisions. Another way of using truthiness in conditionals if we want to make sure that the code will run only if all the conditions are true. Consider the following instantiation.

The if statement will execute while all the conditions are true (string is not empty, the amount is equal to or more than 10000, the age is equal to or more than 18). The else statement will execute even if one of the parameters is false (empty string, the amount is less than 10000, the age less than 18).

Another great example is when we inverse false to true (or vice versa) depending on certain eligibility criteria. Initially, we can set the flight and visaIssued variables into false and, after a certain criterion is met (dollars ≥ 10000), they will be set to true.


Understanding and implementing Boolean values is one of the core principles, both in life and in programming. Feeling comfortable using them as much as possible will make your code more logical, precise, and less error-prone..

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store