Conversion to Number means a Boolean or string type variable is changing it’s type to Number. 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 Number is very easy it could be seen by below given program. |
Example |
<html>
<head> </head> <body> <script type=”text/javascript”> var a,b,c; c= false b= “23” a=20 document.write(“<br>Value of c “+c); document.write(“<br>Value of b “+b); document.write(“<br>Value of a “+a); document.write(“<br>Data type of c “+typeof(c)); document.write(“<br>Data type of b “+typeof(b)); document.write(“<br>Data type of a “+typeof(a)); c=a document.write(“<br>After conversion Data type of c “+typeof(c)); b=a document.write(“<br>After conversion Data type of b “+typeof(b)); </script> </body> </html> |
Understanding program : |
In the above given program a is of number type , b is of string type, c is of Boolean type but when number variable (a) is assigned to b& c they both becomes of number type. |
Output: |
Value of c false Value of b 23 Value of a 20 Data type of c boolean Data type of b string Data type of a number After conversion Data type of c number After conversion Data type of b number |