Conversion to Boolean means a number or string type variable is changing it’s type to Boolean. In JavaScript a variable can contain any type of data and the data contained by the variable represents the type of that variable. Type conversion in JavaScript is done implicitly . The conversion to Boolean is very easy it could be seen by below given program. |
Example |
<html>
<head> </head> <body> <script type=”text/javascript”> var str, num, bool str=”ram”; num=20 bool=true document.write(” <br>type of str is : “+typeof(str)); document.write(” <br>Value of str is : “+ (str)); document.write(” <br>type of num is : “+typeof(num)); document.write(” <br>Value of num is : “+ (num)); document.write(“<br> type of bool is : “+typeof(bool)); document.write(” <br>Value of bool is : “+ (bool)); str=bool num = bool document.write(” <br>type of str is : “+typeof(str)); document.write(” <br>Value of str is : “+ (str)); document.write(” <br>type of num is : “+typeof(num)); document.write(” <br>Value of num is : “+ (num)); document.write(“<br> type of bool is : “+typeof(bool)); document.write(” <br>Value of bool is : “+ (bool)); </script > </body> </html> |
Understanding program : |
bool is having value true of Boolean type, str of type string and num of number type, so str and num will get the value of bool and will become of type Boolean. We have used a method typeof() , which is used to know the data type of any variable given with this method. |
Output: |
type of str is : string Value of str is : ram type of num is : number Value of num is : 20 type of bool is : boolean Value of bool is : true type of str is : boolean Value of str is : true type of num is : boolean Value of num is : true type of bool is : boolean Value of bool is : true |