JavaScript : Boolean Object

Boolean Object and Boolean Primitive Behaviour

Boolean Function

The Boolean() function can be used for converting a value to a boolean value.

// Boolean object
// It represents value as - true or false 

let ob = Boolean(true);
let ob2 = Boolean();
let ob3 = Boolean(false);

let ob4 = true;

console.log(ob,ob2,ob3); // true false false 
console.log(typeof ob, typeof ob4); // boolean boolean
console.log(ob.constructor.name, ob4.constructor.name); // Boolean Boolean 
console.log(ob == ob4, ob2==ob4); // true false 
console.log(ob === ob4, ob2===ob4); // true false

In the above code, the Boolean() acts as a function and it returns a primitive boolean value.

Boolean Object

It is seldom required to create a Boolean Object. The Boolean as a function is predominately used. The Boolean object is created using a new keyword.



let myob = new Boolean(true);
let myob2 = new Boolean();
let myob3 = new Boolean(false);

let myob4 = true;

console.log(myob,myob2,myob3); //Boolean (true) Boolean (false) Boolean (false)
console.log(typeof myob, typeof myob4); // object boolean
console.log(myob.constructor.name, myob4.constructor.name); // Boolean Boolean 
console.log("myob == myob4", myob == myob4, myob2==myob4); // true false 
console.log(myob === ob4, myob2===myob3); //false false

Boolean Object vs Boolean primitive

The boolean primitive is truthy when it has a value of true and falsey when it has a value of false. The Boolean Object value is always considered truthy irrespective of the value.

// Boolean vs boolean 

let myob5 = new Boolean(false);

if (myob5) 
  console.log ("truth value - myob5"); // true

let myob6 = false;

if (myob6)
   console.log("myob6 is true");
else 
   console.log("myob6 is false"); // false

Every object is truthy even if it is empty hence Boolean object is considered truthy.

Complete Code Listing

// Boolean object
// It represents value as - true or false 

let ob = Boolean(true);
let ob2 = Boolean();
let ob3 = Boolean(false);

let ob4 = true;

console.log(ob,ob2,ob3); // true false false 
console.log(typeof ob, typeof ob4); // boolean boolean
console.log(ob.constructor.name, ob4.constructor.name); // Boolean Boolean 
console.log(ob == ob4, ob2==ob4); // true false 
console.log(ob === ob4, ob2===ob4); // true false 


let myob = new Boolean(true);
let myob2 = new Boolean();
let myob3 = new Boolean(false);

let myob4 = true;

console.log(myob,myob2,myob3); //Boolean (true) Boolean (false) Boolean (false)
console.log(typeof myob, typeof myob4); // object boolean
console.log(myob.constructor.name, myob4.constructor.name); // Boolean Boolean 
console.log("myob == myob4", myob == myob4, myob2==myob4); // true false 
console.log(myob === ob4, myob2===myob3); //false false 

// Boolean vs boolean 

let myob5 = new Boolean(false);

if (myob5) 
  console.log ("truth value - myob5"); //true

let myob6 = false;

if (myob6)
   console.log("myob6 is true");
else 
   console.log("myob6 is false"); //false

Did you find this article valuable?

Support Programming with Mahavir by becoming a sponsor. Any amount is appreciated!