New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

JavaScript Boolean Object

JavaScript Boolean object is a member of global objects and a wrapper class. It is used to create a boolean object which holds true or false value, depending upon the value used while creating the Boolean object.

The Boolean object's true and false values are different from the primitive boolean type's true and false values.

As already mentioned, it has two values, true and false. The Boolean object returns false when it is passed with values such as 0, -0, an empty string(""), false, null, undefined, or Not a Number(NaN) while creating the Boolean object. Apart from all these values which set the initial value as false for the Boolean object, all other values, even an empty array([]), empty object({}) or the string "false", will set the initial value for the Boolean object as true.

Creating JavaScript Boolean Object

To create an instance of the Boolean object, we use the new keyword with the Boolean object constructor function, providing a value at the time of creation.

Following is the syntax for it:

let bool = new Boolean(SOME_VALUE);

Depending upon the value passed, the initial value is set as true or false.

Let's take an example,

// Creating Boolean Object
let boolObj = new Boolean(true);  

boolObj = new Boolean(false);  


JavaScript Boolean Object False

JavaScript Boolean Object will have the initial value as false if the value provided at the time of object creation is 0, -0, NaN, null, undefined, false, empty string or even if no value is provided because the default value is also false.

let obj1 = new Boolean();
let obj2 = new Boolean(0);
let obj3 = new Boolean(null);
let obj4 = new Boolean('');
let obj5 = new Boolean(false);

document.write(obj1+" "+obj2+" "+obj3+" "+obj4+" "+obj5)

false false false false false

JavaScript Boolean Object True

Apart from the values specified above, for which the initial value of the Boolean object is false, all other values will set the value as true. Let's take a few examples,

let obj1 = new Boolean(true);
let obj2 = new Boolean('true');
let obj3 = new Boolean('false');
let obj4 = new Boolean('hello');
let obj5 = new Boolean([]);
let obj6 = new Boolean({});

document.write(obj1+" "+obj2+" "+obj3+" "+obj4+" "+obj5+" "+obj6)

true true true true true true

JavaScript Boolean Object vs. Primitive Boolean type

As we have already mentioned that the boolean object and primitive boolean types are different. The Boolean object is a JavaScript object and not a primitive type, but an object type, which can have true or false as its value.

Let's take an example, where we will see how the Boolean object and the primitive boolean type behave when used in a JavaScript conditional statement expression.

// Boolean object
let obj = new Boolean(false);

// using in if condition 
	document.write("It is boolean object"); // executes

// Primitive value
let bool = false;
	document.write("It is primitive boolean"); // does not execute

It is boolean object

In spite of the false value of the Boolean object, the first if statement executes, that is because when we provide an object in the if condition, it is always evaluated as true.

We can get the value of the Boolean objects by using the valueOf() method of the Boolean object and then it will be treated as a norma primitive type true or false value.

Converting Boolean Object to Primitive

We can use the valueOf() method of the Boolean object for accessing its value,

// Boolean object
let obj = new Boolean(false);

// using the value of Boolean object in condition 
	document.write("It is boolean object");   // does not execute
	document.write("boolean value is false");   // executes

boolean value is false

Methods of Boolean Object

The following are some of the commonly used methods of the Boolean object.

  1. toString(): converts the boolean value into a string and returns the string.

  2. valueOf(): returns the primitive value of a Boolean object.

JavaScript Boolean Object Example

Let's see another code example for the Boolean object:

In this tutorial, we have covered the Boolean object, its methods, how it is different from the primitive boolean type along with multiple code examples.