Javascript4

Type Conversion


«<Javascript3|Javascript5»>

Conversion to Boolean

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

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

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

[Top]

Conversion to Number

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

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

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 numbe

[Top]

Conversion to String

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:

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

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

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.

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

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:

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

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

[Top]

Results in a browser

Click conversion to see the result of the implementation of the above codes..
[Top]
«<Javascript3|Javascript5»>

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.