JavaScript boolean data type explained

This tutorial explains how boolean data type and Boolean object works in JavaScript

Posted on May 10, 2021


The boolean data type is a common programming language data type that only has two values: true and false. To declare a boolean variable using JavaScript, you just need to assign either true or false as its value:

const isLoading = true;
const isVisible = false;
console.log(typeof isLoading); // "boolean"
console.log(typeof isVisible); // "boolean"

A boolean variable is usually prefixed with a linking verb like “is”, “can”, or “has”. The boolean values are commonly used to control the flow of your program with the help of conditional statements as in the code below:

if (isLoading) {
  alert("The application is still loading...");
} else {
  alert("The application has finished loading!");
}

You will also get boolean values out of comparison operators like equal to ===, greater than >, and less than < as in the following example:

const isEqual = 3 === 3;
console.log(isEqual); // true
console.log(typeof isEqual); // "boolean"

const isHigher = 9 > 4;
console.log(isHigher); // true
console.log(typeof isHigher); // "boolean"

const isLower = 12 < 4;
console.log(isLower); // false
console.log(typeof isLower); // "boolean"

While the boolean data type is primitive, the Boolean with the capital “B” is an object in JavaScript. Let’s learn the differences between them next.

Boolean object in JavaScript

The Boolean object is a wrapper object that automatically applies to all boolean values you declared. The Boolean object provides your primitive boolean value with methods that could help you with data manipulation.

The two methods provided by Boolean object are toString() and valueOf() methods.

Here’s an example of using the methods of Boolean object:

const hasAccess = true;
console.log(hasAccess.toString()); // "true"
console.log(hasAccess.valueOf()); // true

Finally, you can also create a new Boolean object instance by calling the constructor new Boolean() as in the code below:

const isLoading = new Boolean(true);
console.log(isLoading); // true

JavaScript also has the Boolean function that converts any value you passed as its argument into its boolean equivalent:

const isLoading = Boolean("abc");
console.log(isLoading); // true

const hasAccess = new Boolean(null);
console.log(hasAccess); // false

Since every JavaScript value has a boolean equivalent, any non-boolean value you passed to the Boolean() function or the new Boolean() constructor will be converted into either true or false.

There are 8 values that JavaScript will automatically convert to false:

false	
0	
-0	
0n	// This is BigInt. It follows the same rule as a Number
"" 
null 
undefined	
NaN	

Any other value will be converted to true by JavaScript.

See also: JavaScript truthy falsy values conversion

And that wraps this tutorial on JavaScript boolean type and Boolean object 😉

Related articles:

Grab the free JavaScript book today 👍

Learn the building blocks of JavaScript programming language like data types, functions, objects, arrays and classes.

Use the knowledge from the book to build a small but solid program.

Learn more