javascript - Javascript boolean

Javascript Booleans: True or False Values

A core mechanic of Javascript is the ability to distinguish between true and false values. The Javascript standard defines true and false values as a unique data type called a Javascript boolean.

Javascript booleans may be true, false, or (in certain contexts) a value that evaluates to either true or false. It is important to manage boolean contexts properly because many Javascript constructs rely on them.

This article describes what Javascript booleans are and how to use them appropriately in different programming contexts.

Javascript Boolean Basics

Javascript booleans are a primitive type, which means they do not need to be explicitly created as an object. Use the reserved keywords true or false to assign a boolean value to a variable. The same logic applies when creating a boolean in JSON.

let example1a = true;
let example1b = false;

Never explicitly specify a boolean value as a string or as an object. Strings are not the correct data type for boolean expressions, while creating booleans as objects results in unnecessary overhead and complicates code.

let example2a = "true";             // Incorrect: not a boolean.
let example2b = new Boolean(true);  // Incorrect: overcomplicated.

Javascript control statements that only execute under certain conditions rely on booleans to evaluate those conditions. This is a vital mechanism in structures including while loops and if else statements, as the following example demonstrates. In this example, assume that someBoolean() is a Javascript function that returns a boolean value.

// While loop with boolean end condition
let example3a = someBoolean();
while (example3a === true) {
    ... instructions ...
}

// If else statement
let example3b = someBoolean();
if (example3b === true) {
    ... instructions if true ...
} else {
    ... instructions if false ...
}

‘Truthy’ and ‘Falsy’ Values

Implicit Javascript booleans are values that evaluate to true or false in the context of a Javascript language structure like a while loop. These implicit values, since they’re not strictly true or false, are commonly known as “truthy” or “falsy” (or “falsey”).

“Truthy” values usually come from objects with a defined value or structure. For example, an empty array, most numbers, empty objects, and non-empty strings (including those composed of only whitespace) evaluate to an implicit true. The following examples show some less intuitive “truthy” values.

// More examples of "truthy" values
let example4a = "false";  // The string "false"
let example4b = -2;       // A negative number
let example4c = {};       // An empty object

“Falsy” values usually come from objects without a defined value or structure. For example, null values, undefined values, and the empty string evaluate to an implicit false. The following examples show some less intuitive “falsy” values.

// More examples of "falsy" values
let example5a = -0;   // Negative 0
let example5b = NaN;  // Value for Not A Number
let example5c = 0n;   // BigInt 0

Assume that all values are “truthy” unless they are explicitly defined as “falsy.” Explicitly defined “falsy” values can sometimes vary between browsers and Javascript implementations, so when in doubt, consult the available documentation.

Managing Javascript Booleans

Since Javascript booleans can be explicit or implicit, you should always be aware whether you are using strict Javascript operators to evaluate boolean expressions. “Truthy” and “falsy” expressions only evaluate as equal to explicit booleans if you are not using strict comparisons.

In this example, the first comparison considers the string “1” equal to true because it uses the non-strict equals operator (==). The second comparison considers the string “1” unequal to true because it uses the strict equals operator (===).

// evaluates to true because a non-empty string is "truthy"
let example6a = ("1" == true);

// evaluates to false with strict comparison
let example6b = ("1" === true);

The flexibility of using non-strict comparisons with “truthy” and “falsy” expressions can be both helpful and harmful. It may make programming easier, but maintenance may be tougher in the long term because it could be hard to tell which values are being compared.

Conclusion

Javascript booleans are a core data type that many other parts of Javascript rely on. Distinguishing between explicit booleans and “truthy” or “falsy” values is a common source of programming errors, so managing booleans properly is important.

Enroll in our Intro to Programming Nanodegree Program today to learn more about Javascript booleans and other programming concepts.

Jessica Reuter Castrogiovanni
Jessica Reuter Castrogiovanni