Conversion to String means a number or Boolean type variable is changing it’s type to String. 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 String 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=b document.write(“<br>After conversion Data type of c “+typeof(c)); a=b document.write(“<br>After conversion Data type of a “+typeof(a)); </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 string variable (b) is assigned to a & c they both becomes of string 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 string After conversion Data type of a string |
Click here to view result of this program on browser |
ParseInt():- |
This function is used to convert a number data of string type to number type. |
If there are two numbers of string type data and we want to perform arithmetic calculation with them then we should use parseInt or parseFloat() functions to convert the type of values to number because the arithmetic calculations can be performed only with number type data. |
<html>
<head> </head> <body> <script type=”text/javascript”> var a,b,c; c= “15” b= “25” a=20 document.write(“<br>Value of c “+c+” type of c is “+typeof(c)); document.write(“<br>Value of b “+b+ “type of b is ” + typeof(b)); document.write(“<br>Value of a “+a+”type of a is “+typeof(a)); a = c+b+a document.write(“<br>without using any conversion function result of a=a+b+c is “+a); a=20 a = parseInt(c)+parseInt(b)+parseInt(a) document.write(“<br>After using parseInt function result is “+a); </script > </body> </html> |
output is: |
Value of c 15 type of c is string Value of b 25type of b is string Value of a 20type of a is number without using any conversion function result of a=a+b+c is 152520 After using parseInt function result is 60 |
ParseFloat() :- |
This function is used to convert a floating number of string type to number type. |
Example: |
<html>
<head> </head> <body> <script type=”text/javascript”> var a,b,c; c= “15.25” b= “25.62” a=20.45 document.write(“<br>Value of c “+c+” type of c is “+typeof(c)); document.write(“<br>Value of b “+b+ “type of b is ” + typeof(b)); document.write(“<br>Value of a “+a+”type of a is “+typeof(a)); a = c+b+a document.write(“<br>without using any conversion function result of a=a+b+c is “+a); a=20.45 a = parseFloat(c)+parseFloat(b)+parseFloat(a) document.write(“<br>After using parseInt function result is “+a); </script > </body> </html> |
Output is: |
Value of c 15.25 type of c is string Value of b 25.62type of b is string Value of a 20.45type of a is number without using any conversion function result of a=a+b+c is 15.2525.6220.45 After using parseInt function result is 61.32000000000001 |