How to convert a value into Boolean in JavaScript?


In this article we are going to discuss how to convert a value into Boolean in JavaScript.

In JavaScript programming we sometimes need values like these ‘YES / NO, ‘ON / OFF’, ‘TRUE / FALSE’. So, JavaScript provides Boolean() method, which helps us to convert a value into Boolean. A Boolean is a value that can return either true/false. There are two ways using which we can convert values into Boolean. One way is to use Boolean() method and the other way is to use the !! notation.

Let’s understand this concept better with the help of the examples further in this article.

Syntax

The syntax to display the conversion of value to Boolean using Boolean method and operator is shown below.

Boolean(value) or !!(value)

Example 1

The following is an example program to convert a value to Boolean using Boolean() method.

<html>
<body>
   <script>
      const isTrue = 'Golden State Warriors';
      document.write(new Boolean(isTrue));
      document.write("</br>");
      document.write(Boolean(isTrue));
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 2

The following is another example program to convert a value to Boolean using Boolean() method.

<!DOCTYPE html>
<html>
<head>
   <title>To display a value in boolean</title>
</head>
<body style="text-align : center">
   <h3>Convert a value into Boolean</h3>
   <p id="bool"></p>
   <script>
      var value1 = 0;
      var value2 = '';
      var value3 = null;
      var value4 = 'Hello';
      var value5 = 1234;
      document.getElementById("bool").innerHTML = "The boolean value for the value (0) is : "+Boolean(value1)+'<br/>'+"The boolean value for the value ('') is : "+Boolean(value2)+'<br/>'+"The boolean value for the value (null) is : "+Boolean(value3)+'<br/>'+"The boolean value for the value ('Hello') is : "+Boolean(value4)+'<br/>'+"The boolean value for the value (1234) is : "+Boolean(value5);
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 3

Let’s look at an example program to convert a value to Boolean using !! notation.

<!DOCTYPE html>
<html>
<head>
   <title>To display a value in boolean</title>
</head>
<body style="text-align : center">
   <h3>Convert a value into Boolean using !! Notation.</h3>
   <p id="bool"></p>
   <script>
      var value1 = 0;
      var value2 = "";
      var value3 = NaN;
      var value4 = 'Tutorials Point';
      var value5 = 1234;
      document.getElementById("bool").innerHTML = "The boolean value for the value (0) is : "+!!value1+'<br/>'+"The boolean value for the value ('') is : "+!!value2+'<br/>'+"The boolean value for the value (NaN) is : "+!!value3+'<br/>'+"The boolean value for the value ('Tutorials Point') is : "+!!value4+'<br/>'+"The boolean value for the value (1234) is : "+!!value5;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Updated on: 08-Dec-2022

661 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements