Javascript type conversion example

  • 2020-03-30 02:43:59
  • OfStack


<script>
    var i="123abc";
    i=parseInt(i);//String transformation
    alert(i+","+typeof(i));//Output: 123, number
    i="12.3abc";
    i=parseFloat(i);//String to float
    alert(i+","+typeof(i));//Output: 12.3, number(so both int and float are of type number)
    i="a123abc";
    i=parseInt(i);//String transformation
    alert(i+","+typeof(i));//Output: NaN, number (" Not a number, Not a number "because the conversion failed)
    var num=document.getElementById("num").value;
    function showMsg(num)
    {
        for(var i=0;i<num;i++)
        {
            document.write(" hello JavaScript ! <br/>");
        }
    }
</script>


Related articles: