techStackGuru

JavaScript Boolean


Booleans are primitive data types in JavaScript that can be either true or false.

The JavaScript Boolean object can also be created with the Boolean() constructor.

Syntax


Boolean bool = new Boolean (value);  

Lets take an example


console.log(5 === '5'); //false
console.log(6 == '6');  //true

Boolean Methods

MethodDescriptionExample
toString()Boolean to string conversion

let bool = false;
let val = bool.toString(); 
console.log(typeof val)  //string
valueOf()Provides the boolean primitive value

let bool = true;

// convert to string
let val = bool.valueOf();

console.log(bool); //true
console.log(typeof val); //boolean

Boolean Objects

You may also use the new keyword to construct a boolean value.


const x = true;

// creating a boolean object
const y = new Boolean(false);

console.log(x); // true
console.log(y); // false

console.log(typeof x); // "boolean"
console.log(typeof y); // "object"

previous-button
next-button