Data types exist to somehow be able to see the difference between numbers and strings, for example. In general, basic types include numbers, strings, undefined, null, and boolean data types.

From titles we can apprehend what it entails: numbers include integers, decimal numbers; strings are any data in quotation marks, indefinite type for when no value is specified. And what is a Boolean data type?

Boolean is very often and widely used. It will be useful for controlling the progress of the program using conditional statements (if else, switch, while, etc). It can only confirm or deny the truth of the expression: true or false.

It can also be used to easily get the value of a variable, object or expression. If we use one of these operators <, >, <=, >=, ==, ===, !=, !== we will also get true or false as a result.

By the way, in mathematics, 0 and false, 1 and trueare often identified. Since this data type takes only 2 values, it is also called a logical data type.

Syntax and applications

Let's declare variables of type boolean:

const x = true;
const y = false;

You can easily check which data type in these variables with the typeof operator. As a result, we do see that these variables are boolean.

typeof(x);  // boolean
typeof(y);  // boolean

By the way, it is important to note that if we write the word true or false in quotation marks, the program will not consider this variable the boolean type, but a string.

const x = "true";
typeof(x);  // string

In addition to being able to declare boolean type variables on our own, there are more uses. All comparison expressions also return logical values. Almost every time we write a if or switchcondition in a program, we unconsciously use a boolean data type.

for (let i = 0; i <= 10; i++) {
 if (i % 2 === 0) {
   console.log(i, ' - is an even number');
 } else if (i % 2 === 1) {
   console.log(i, ' - is an odd number');
 } else {
   console.log('Something wrong.');
 }
}
// Output:
// 0  - is an even number
// 1  - is an odd number
// 2  - is an even number
// 3  - is an odd number
// 4  - is an even number
// 5  - is an odd number
// 6  - is an even number
// 7  - is an odd number
// 8  - is an even number
// 9  - is an odd number
// 10  - is an even number

In this example, the program mostly uses the boolean data type. In the following example we will use loop while. This cycle will continue until the counter is less than one. If the counter becomes less than 1, the value of true will change to the false value.

let x = 100;
 
while (x > 1) {
 x /= 2;
 console.log(x);
}
 
// Output:
// 50
// 25
// 12.5
// 6.25
// 3.125
// 1.5625

Firstly e wrote a loop that will go from 0 to 10. We take the first number 0 and check whether it is divisible by 2 without remainder. The result will be true, so we get the message "0 - is an even number".

On the next step we check the number 1. The condition i % 2 == 0 will be false, so this code block will not be executed. The next step is to check whether the number when divided by 2 gives the remainder 1. True as a result, so we will see the message "1 - is an odd number" on the screen.

In a similar way we will check all following numbers. By the way, if suddenly at some step the first and second conditions will output a false value, we will see a message "Something wrong" on the screen.

No matter what value we pass to a variable of type boolean, this variable will still have exactly one value: true or false. Even if we just declare a variable without initializing it, or if we give it an empty string. Let's look at a few examples.

Boolean(2)              // true as a result
Boolean(2.3)            // true
Boolean(1)              // true
Boolean(0)              // false
Boolean(123456.123456)  // true
Boolean(true)           // true
Boolean(false)          // false
Boolean(" ")            // true
Boolean("true")         // true
Boolean("false")        // true
Boolean(undefined)      // false
Boolean(null)           // false
Boolean({})             // true
Boolean([])             // true
Boolean([1,2])          // true
Boolean([1,[2,3]])      // true
Boolean(2 + 3)          // true
Boolean(2 < 3)          // true
Boolean(2 > 3)          // false
Boolean(2 != 3)         // true
Boolean(2 === 2)        // true
Boolean()               // false
Boolean(2 / 0)          // false
Boolean(2 / 'a')        // false

Boolean() object

In addition to the data type, there is a JS object with the same name. We need to know the difference between boolean as a primitive data type and Boolean() as an object. A Boolean object is a boolean logical data type shell, or in other words, this object is a wrapper for boolean values.

In order to declare a value as a Boolean object, it is necessary to write as follows:

const x = new Boolean(value);
const x = Boolean(value); // keyword "new" also can be omitted

However, experts do not recommend defining boolean values as objects, as it significantly slows down code execution speed. In addition, the new keyword in this case complicates the code and can lead to unexpected results.

An illustrative example in this regard:

const x = true;
const y = new Boolean(true);
 
typeof(x)       	// boolean
typeof(y)       	// object
 
if(x === y) {}  	// will return false, because x is a boolean value, y is an object value

And now another interesting example that will return an unpredictable result.

const x = new Boolean(true);
const y = new Boolean(true);
 
if (x == y) {
 console.log('expression 1');
}
if (x === y) {
 console.log('expression 2');
}
   // No output returned

What will we see on the screen if one fulfills the last two conditions? This time the last two expressions will return a false result. But why, if here the values are the same and the two values are objects? Because in JavaScript, comparing two objects always returns false as a result.

In addition, the Boolean() object includes the following methods:

  • toLocaleString() - able to return a string of logical values in the local environment;

  • toString() - helps convert the input value of a boolean value to string;

  • valueOf() - useful when you want to find the value of a Boolean object.