JavaScript Boolean valueOf()

Posted by

Method

JavaScript Boolean valueOf() Method

Introduction

The JavaScript Boolean valueOf() method is used to return the primitive value of a Boolean object. It is a method of the Boolean object and can be called directly on the Boolean object.

Syntax

The syntax for the JavaScript Boolean valueOf() method is as follows:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Boolean.valueOf(value)

[/dm_code_snippet]

Parameters

The JavaScript Boolean valueOf() method takes in one parameter:

  • value: The value whose primitive value will be returned.

Return Value

The JavaScript Boolean valueOf() method returns a primitive Boolean value (true or false).

Example

The following example demonstrates the JavaScript Boolean valueOf() method:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

// Create a Boolean object
var myBool = new Boolean(true);
 
// Get the primitive value of the Boolean object
var boolVal = myBool.valueOf();
 
// Output the value
console.log(boolVal); // Outputs: true

[/dm_code_snippet]

In this example, we create a Boolean object with the value true. Then, we call the valueOf() method on the Boolean object, which returns the primitive value (true) of the Boolean object.

Conclusion

In this tutorial, we discussed the JavaScript Boolean valueOf() method. We looked at the syntax of the valueOf() method and discussed its parameters and return value. We also looked at an example of using the valueOf() method.