What are the methods of a boolean object in JavaScript?



In this tutorial, we will learn what are the methods of a boolean object in JavaScript.

First, we will learn about what is a boolean object. The boolean object represents two values: ‘true’ and ‘false’. The boolean object’s default value is false if the value parameter is missing, 0, -0, null, false, NaN, undefined, or the empty string (‘’). The Boolean object value will be set to true for any other values. The boolean objects have some properties such as constructor and prototype and some methods like toString() and valueOf(). Here we will emphasize these two methods.

Users can follow the below syntax to initialize a Boolean object.

Syntax

const boolean_object = new Boolean(value/expression)

In the above syntax, as the user can see, we are creating a boolean object named ‘boolean_object’ and passing value or expression as arguments.

Properties

  • constructor − This property returns the reference of the boolean function that created the Boolean object

  • prototype − This property allows adding properties and methods in the Boolean prototype.

Methods

  • toString() − This method helps to convert Boolean into a string.

  • valueOf() − This method returns the value of the boolean object.

Using toString() method

Example

In the example below, we used two Boolean object constructors, “bool1” and “bool2”, and passed values true and false, respectively, to create Boolean objects. Then we display the type of these two boolean variables, i.e., objects. After clicking the “Click here to change type” button, the function to_string() is invoked, and there we changed the boolean into a string and stored the value in the strBoolean and strBoolean2 variables, respectively, using the toString() method. After that, the two boolean objects type changed into a string. As we can see in the output after clicking the button, the type of those values is changed.

<html> <body> <h2> Using the <i> toString() method</i> of Boolean object in JavaScript </h2> <button onclick="to_string()"> Click here to change type </button> <div style="border: 1px solid grey; background-color: rgb(184, 244, 224); margin: 10px 0px; padding: 10px;"> <p id="true"> </p> <p id="false"> </p> </div> <script> const bool1 = new Boolean(true) const bool2 = new Boolean(false) document.getElementById("true").innerHTML = bool1 + ' (type: ' + typeof(bool1) + ')' document.getElementById("false").innerHTML = bool2 + ' (type: ' + typeof(bool2) + ')' function to_string() { let strBoolean = bool1.toString() let strBoolean2 = bool2.toString() let root = document.getElementById("true") let root2 = document.getElementById("false") root.innerHTML = strBoolean + ' (type: ' + typeof(strBoolean) + ')' root2.innerHTML = strBoolean2 + ' (type: ' + typeof(strBoolean2) + ')' } </script> </body> </html>

Using valueOf() method

Example

In the below example, we are taking an expression from the user using an input tag in HTML. Then after clicking the “Check” button, the function checkExpression() is invoked in the click event. Here the value of the input expression is stored in the expression variable. After that, we evaluate that expression using the eval() method. After analyzing the function, the result is stored in a bool object. Then we display the expression and the value of the bool object using the valueOf() method. As we can see in the output, we are checking whether the numeric 1 and the string ‘1’ are equal or not, showing the false value.

<html> <body> <h2> Using the <i> valueOf() method </i> of Boolean object in JavaScript </h2> <p> Please input an expression below and click "Check" button</p> <input type="text" id="expression-input" name="expression" value="1==true" /> <button onclick="checkExpression()"> Check </button> <div id="root" style=" padding: 15px; margin-top: 5px; background-color: rgb(244, 250, 159); border: 2px solid orange; " > </div> <script> // root element const root = document.getElementById('root') // input field const input_field = document.getElementById('expression-input') // 'Check' button click event handler function function checkExpression() { // input field's value let expression = input_field.value // creating Boolean object const bool = new Boolean(eval(input_field.value)) const bool_value = bool.valueOf() document.getElementById('root').innerHTML = expression + ' : ' + bool_value } </script> </body> </html>

In this tutorial, we have learned about the methods of a boolean object in JavaScript. We mainly discussed two methods: the toString() and the valueOf(). toString() is used to convert the boolean data type into a string. And the valueOf() method returns the primitive Boolean value.


Advertisements