Converting Strings to Numbers
The following table list all three functions we can use to convert string to a number:
Method | Description |
---|---|
Number(<str>) | Parses the string to create an integer or real value |
parseInt(<str>) | Parses the string to create an integer value |
parseFloat(<str>) | Parses the string to create an integer or real value |
<!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>