JavaScript Boolean toString() Method

Javascript Boolean toString() method is used to return a string, either “true” or “false”, depending on the value of the boolean object.

Syntax

boolean.toString()

Parameters

It doesn’t accept any parameter.

Return Value

It returns a string, either “true” or “false”.

Example 1: Usage of Boolean toString()

let data = new Boolean(79); 
let data2 = new Boolean("askjavascript"); 

console.log(data.toString()); 
console.log(data2.toString());

Output

true
true

Example 2: Boolean object with value 0

let data3 = new Boolean(0);
console.log(data3.toString());

Output

false

Example 3: Boolean object with value -1

let data4 = new Boolean(-1);
console.log(data4.toString());

Output

true

Example 4: Boolean object with value 1

let data5 = new Boolean(1);
console.log(data5.toString());

Output

true

Example 5: Boolean object with a true and false value

let data6 = new Boolean(true);
let data7 = new Boolean(false); 
 
console.log(data6.toString());
console.log(data7.toString());

Output

true
false

Example 6: Boolean object with conditional expression

let age = 24;
let isAdult = new Boolean(age >= 18);
let result = isAdult.toString();
console.log(result);

Output
true

Example 7: Boolean object with complex value

let data9 = new Boolean(2 + 4kl);
console.log(data9.toString());

Output

SyntaxError: Invalid or unexpected token

Browser Compatibility

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

That’s it!

Related posts

JavaScript Boolean() Constructor

JavaScript Boolean valueOf()