JavaScript Boolean: Understanding the Concept and Uses

JavaScript is different from other programming languages in terms of data types, it is dynamically typed, allows changing the data type of a variable during runtime, it uses the concept of loose equality, i.e, it compares value regardless of what data type is, even it allows to declare a variable without any keyword like var, let, and const. 

In JavaScript, there are mainly 6 primitive data types:

  1. Number: It is a Numeric value(i.e integer, float, etc).
  2. Boolean: It is True or False.
  3. String: It is a sequence of characters.
  4. Symbol: It is a unique identifier.
  5. null: It is a special value that represents the absence of a variable.
  6. undefined: It is a variable that is declared but has no value.

In this tutorial, you will learn about Boolean.

Also Read: JavaScript null and undefined

JavaScript Boolean Data Type

JavaScript has a Boolean object and a Boolean primitive data type, both can hold either True or False. This helps in conditional execution, for instance, we have created a boolean variable email_verified, that can hold True or False, if the entered email is verified it contains True if not then False, this help in checking the execution of the next function which can only send an email if the email_verified variable contains the value True.

Let’s see a practical example for more clarification.

let email_verified = false; // Boolean variable to check if email is verified or not

function sendEmail() { // Function to send email
    if (email_verified) {
        console.log("Email sent");
    } else {
        console.log("Email not verified");
    }
}

sendEmail(); // Calling the function

email_verified = true; // Changing the value of email_verified to true

sendEmail(); // Calling the function again

Output:

Email not verified
Email sent

Here we have declared boolean variables with the initial value false, then created a function that will print “Email sent” if the value is True and “Email not verified”, if it is false. Then we called that function, and you can see it prints “Email not verified”, but then we changed the value of the boolean variable to true, then called the function again, and this time it prints ”Email sent”, hence the output is justified.

In real-time development, there is some functionality to decide whether the value is true or false based on some verification process. 

JavaScript Boolean() Method

Javascript have a method Boolean() which is used to convert any type into a boolean type, it is also used to find out if an expression or a value is true or false.

Example:

const exp = 4 > 2;

const result = Boolean(exp);

console.log(result);

Output:

true

In the above example, you can see that the expression passed to this method is mathematically correct so it returns true.

Everything with a value is True

Boolean() returns true if the argument passed is any value.

Example:

Boolean(1); // true
Boolean("abc"); // true
Boolean({}); // true
Boolean([]); // true

Note: 0 is not considered a value, it represents Null.

Everything without a value is False

Boolean() returns false for, ” “, undefined, null, 0, and NaN.

Example:

Boolean(0); // false
Boolean(""); // false
Boolean(null); // false
Boolean(undefined); // false
Boolean(NaN); // false

JavaScript Boolean Object

The only difference between the boolean object(i.e Boolean) and boolean(lower case) is the data type, the Boolean is considered an object whereas the boolean is a primitive data type.

Boolean Objects can be created using a new keyword with Boolean().

Syntax:

new Boolean()

Example:

const bool = true;

const boolObj = new Boolean(true);

console.log(typeof bool); // boolean 
console.log(typeof boolObj); // object

Output:

boolean
object

In the above output, we got the boolean data type for the boolean declared without a new keyword, and the object data type for the boolean created using a new keyword.

The typeof() method just returns ‘object’, not the exact object type, we have a separate tutorial on How To Get Type Of Object In JavaScript in which we have covered how can you find the exact type of object if you want to read.

JavaScript Boolean Methods

Below are some built-in JavaScript boolean methods.

toString() Method

This method returns a string containing the boolean value. The data type of return value is always a string.

Example:

const boolObj = new Boolean(true);

const result = boolObj.toString(); // "true"

console.log(typeof result);

Output:

string

In the above output, you can see the data type of return value is a string.

valueOf() Method

This method returns the value of a boolean variable. The data type of return value is boolean.

Example:

const boolObj = new Boolean(true);

const result = boolObj.valueOf(); // Returns the primitive value of the Boolean object

console.log(typeof result);

Output:

boolean

In the above output, you can see the data type of return value is boolean.

Summary

Boolean can be an object which is created using new Boolean() and can be a primitive data type that can contain whether true or false. Boolean values are helpful in conditional rendering. It is mainly used with if-else and switch-case where it is required to keep track of something. Hope this tutorial helps you to understand the Boolean in JavaScript.

Reference

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean

Aditya Gupta
Aditya Gupta
Articles: 109