How to convert a value into Boolean in JavaScript?

JavascriptFront End TechnologyObject Oriented Programming

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.

Live Demo

<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.

Live Demo

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

Output

true
raja
Published on 30-Jul-2019 08:22:34
Advertisements