Computer >> Computer tutorials >  >> Programming >> Javascript

How to convert a value into Boolean in JavaScript?


There are three ways to convert a value into boolean. In those 3 methods, 2 methods include the Boolean keyword in them whereas the other method is a new method in which a symbol !! is used. Let's discuss them in detail.

Using Boolean keywod

Example

In the following example, the methods which use the Boolean keyword in them were implemented and the result is displayed as shown in the output.

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

Output

true
true


Using !!

Example

In the following example, instead of Boolean keyword a symbol(!!) is used to convert a value to Boolean.

<html>
<body>
<script>
   const isTrue = 'Golden State Warriors';
   document.write(!!isTrue);
</script>
</body>
</html>

Output

true