What Is a Boolean Data Type, and What Are Some Uses?

Share this article

Truthy and Falsy: When All is Not Equal in JavaScript

Learn what the Boolean data type is, and how to use it in programming.

This article details the definition of a Boolean data type and explains its use in programming languages such as Python. There are also examples of Boolean operators that will be useful when understanding Boolean logic and conditional statements.

What is a Boolean data type?

In computer programs, there are three types of data: text, numbers and Booleans. A Boolean data type is a value that can only be either true or false (these are known as Boolean values).

A true Boolean value might indicate that the object is valid (e.g. an email address has been typed correctly). A false Boolean value indicates that the object is invalid and has not been done correctly (e.g. you’ve forgotten to fill out a required field).

Boolean values are true and false values

Boolean values have two possible states: true and false. In binary, these are represented by 1 and 0.

Boolean algebra is a type of math that deals with logical operations on logical values, including binary variables. It is the foundation for decisions in programs using logical operators, so it’s important to understand how Booleans work.

How are Boolean values used in computer programming?

A Boolean value is used to create conditions and control how a program behaves when certain things happen (e.g. if a condition is true, then do something). They can have only two possible values: either 0 or 1. You cannot add or subtract them. A Boolean variable is a special type of memory in a computer that can only store two values: true or false.

What are the differences between text, numbers and Booleans?

When using Booleans in programming, it’s important to understand the differences between a Boolean variable and other types of data. You need to know how they are stored, and what operations can be performed on them.

Booleans

Booleans are usually stored using one byte of memory while text variables use more than one (e.g. two bytes for ASCII, four bytes for Unicode).

Text

Some programming languages represent text with an array of characters (e.g. ASCII or Unicode). Text data types have a limited size (typically 256 characters) and do not perform operations on themselves.

Numbers

Numbers can be negative, positive, or zero depending on their properties. They are stored as an array of bits which determines if they’re positive (i.e.: 00000000). The same applies to negative numbers (-1, -2, etc).

These types of values can be used in mathematical operations such as addition and subtraction. Booleans cannot be added or subtracted from each other because they only have two states: true and false.

Truthy and falsy values

Clouds of various data types depicting whether they evaluate to truthy or falsy when evaluated with a Boolean operator

There are some special values in programming languages which can be treated as both text and Booleans. These are known as “truthy” or “falsy” values, depending on whether they evaluate to true or false respectively.

For example, 0 is a falsy value because it evaluates to false but “0” is a truthy value as a defined string.

Get to know how various variable states and data types will evaluate to true or false by reading our guide to truthy and falsy in JavaScript.

Boolean operators

When using Booleans in a program, it’s important to understand the Boolean operators. These are used in conditions and conditional statements which control how the program will behave. Logical operator examples include AND (&&), OR (||) and NOT (~).

Boolean operator examples

The following are examples of the Boolean operators in programming:

  • >= – True if a number is greater than or equal to another.
  • <= – True if a number is less than or equal to another.
  • == – True if two values are equivalent.
  • != – True if two values are not equivalent.
  • && – True if both values are true.
  • || – True if either of the values are true.
  • ! – True if the value is false.
  • ~ – Reverses all of the bits in a variable (e.g.: 0000000000000000 becomes 1111111111111111). This can be useful when necessary because it allows you to change Booleans without affecting values or other types of data. It can also be used to revert changes made by a previous statement (e.g.: if (!k) then k = 1).

Boolean operators are used to make decisions in programs and indicate how the program should behave. For example, if p is true AND q is also true, then do something.

Boolean use-case example

Boolean values are used in conditional tests as discussed below.

  • Checks that the email address is valid.
  • Checks that the password is at least 6 characters long.
  • Checks that both fields are filled out correctly.

What is an example of a Boolean data type in practice?

The following is an example of a Boolean in C++ code (note that the type of variable is not represented and can be stored using any variable data type):

This function returns true if two numbers add up to zero, otherwise false.

bool NumberCheck(int x, int y) { return x+y == 0; };

Boolean data types can also be used as operands in relational (e.g.: “is greater than”) and equality (e.g.: “equals” or “does not equal”) operators to perform such tasks as comparing directory contents and checking file sizes from a website, as shown in JavaScript below:

if (document.getElementById("files").value) {
    if (parseInt(document.getElementById("files").value) > 1048576) {
      alert("You have selected a file larger than 1MB.");
    } else {
      alert("You have selected a file smaller or equal to 1MB.");
    }
  } else {
    alert("Please select at least one file.");
  }

History & Origins of the Boolean Data Type

Booleans are named after George Boole, who was a mathematician from the 19th century. He first developed Boolean algebra in 1854.

The Boolean data type was invented in the early 1800s. George Boole created a system of logic that could be used to describe the true values (i.e.: 1) and false values (i.e.: 0) in computers. This developed into what we now call Boolean algebra, which is used as a basis for most computer programming languages.

In 1854, George Boole wrote “An investigation into the Laws of Thought on Which are Founded the Mathematical Theories of Logic and Probabilities”, which laid the mathematical foundation for many computer languages.

Booleans were first used commercially in 1951 by a Bell Labs engineer as a part of AN/FSQ-7, the massive computer that was used by the United States Air Force.

FAQs

What are Boolean variables in programming?

The Boolean data type is used to store the logical values true and false. This data type may be used to store information that allows one of two states, on or off, to be stored.

What is a nullable data type?

A nullable type is used to represent values that may be set to the null (no value) state.

What is a null value?

null is the data type used to represent objects (e.g., variables, records, etc.) which do not currently exist, are unfinished/unused, or are being discarded so that resources will be freed. It could also be used when a Boolean expression evaluates to nothing and needs to be given a value. It returns false.

What is a non-zero value?

A non-zero value is a value that is not zero. It returns true.

Can a Boolean data type have the value of null?

No, it cannot, but you can use an undefined Boolean value to represent a null value. If checked against an operator, it returns false.

What is a Boolean data type in a database?

Boolean data types can be used to store the values true and false in a database.

Booleans are most commonly used in databases to represent yes/no, on/off, or other related states. For example, if an account has been turned off then its status column may contain false. If it’s currently on then true would be stored in the account status Boolean. Another common use is for flags which allow you to store information about what happened (e.g.: ’email sent’, ‘file downloaded’).

Booleans may also be useful to show if a file exists, or if an action has been performed (e.g.: form submitted).

What are some programming languages that support Boolean data types?

Booleans are available in most programming languages. If you’re using JavaScript, Java, PHP, Python, C, C++ or Swift then you should have access to the Boolean data type.

If null is allowed in your programming language, then both true and false would be allowed as well.

What are some applications that manipulate Boolean data types?

Booleans can be used by application software to manage flags, yes/no options, on/off states and other related information. They may also be useful in keeping track of resources or performing various tasks (e.g.: deleting files).

Summary

In this blog post you learned what Boolean values are and how they differ from other types of data. You also learned why it’s important to understand how Booleans work, and how to use Boolean operators in programming.

Joel FalconerJoel Falconer
View Author

Joel Falconer is a technical content strategist. He has been managing editor at SitePoint, AppStorm, DesignCrowd, and Envato, and features editor at The Next Web.

booleandata typesfalsytruthy
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form