Converting Strings to Numbers - Javascript Language Basics

Javascript examples for Language Basics:Introduction

Introduction

String-to-Number Functions

Method Description
Number(<str>) Parses the specified string to create an integer or real value
parseInt(<str>) Parses the specified string to create an integer value
parseFloat(<str>) Parses the specified string to create an integer or real value

You can convert string to number with the Number function.

Demo Code

ResultView the demo in separate window

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
    </head>
    <body>
        <script type="text/javascript">

            var firstVal = "5";
            var secondVal = "5";

            var result = Number(firstVal) + Number(secondVal);

            document.writeln("Result: " + result);
        </script>
    </body>
</html>//  ww w.j  av a  2s  .c o m

Related Tutorials