a) Converting String to Integer
To convert a value from string to integer we have to use the function or method "parseInt().
Passing a correct argument to the function (e.g: "4") will return correct value else (e.g: "sd") 0 will be returned.
Example
Result
parseInt("4");
4
parseInt("5aaa");
5
parseInt("4.33333");
4
parseInt("aaa");
NaN (means "Not a Number")
b) Converting Float to Integer
To convert a float value in to integer we have to use the function or method parseInt.
Example
Result
parseInt(5.133);
5
Example Code:
<script language="javascript">
var a = "334";
var b = 3;
var c = parseInt(a)+b;
var d = a+b;
document.write("parseInt(a)+b = "+c);
document.write(" a+b = "+d);
</script>
Result:
A Note
Javascript (JS) is one of the most used languages in the world. Some times spelled as Java Script.
Hope you enjoy this tutorial. Do send your feedback or suggestions on this javascript or java script tutorial. This is a copyright content.