JavaScript Boolean valueOf() Method

JavaScript Boolean valueOf() method is used to return a boolean value, either true or false, depending on the value of the boolean object.

Syntax

boolean.valueOf()

Parameters

It doesn’t accept any parameter.

Return Value

It returns a boolean value, either true or false.

Example 1: Usage of Boolean valueOf()

let data = new Boolean(79);
console.log(data.valueOf());

let data2 = new Boolean("askjavascript");
console.log(data2.valueOf());

Output

true
true

Example 2: Boolean object with value 0

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

Output

false

Example 3: Boolean object with value -1

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

Output

true

Example 4: Boolean object with value 1

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

Output

true

Example 5: Boolean object with a true and false value

let data6 = new Boolean(true); 
console.log(data6.valueOf());

let data7 = new Boolean(false); 
console.log(data7.valueOf());

Output

true
false

Example 6: Boolean object with conditional expression

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

Output
true

Example 7: Boolean object with Empty String

let data8 = new Boolean(""); 
console.log(data8.valueOf());

Output

false

Example 8: Boolean object with complex value

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

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

Boolean() Constructor

Boolean toString()