Home >Web Front-end >JS Tutorial >How to Convert String Input from Prompt Boxes to Integers in JavaScript?
Numeric Input from Prompt Box: Conversion into Integers
In the pursuit of performing mathematical calculations using HTML, jQuery, and JavaScript, it's often necessary to obtain input from the user. However, prompt boxes return values as strings, hindering any numerical computations.
To convert these string values into integers, JavaScript provides the following methods:
Syntax:
parseInt(string, radix); parseFloat(string);
where:
Example:
var x = prompt("Enter a Value", "0"); var y = prompt("Enter a Value", "0"); var num1 = parseInt(x); var num2 = parseInt(y);
After conversion, these values can be used for mathematical calculations as desired.
The above is the detailed content of How to Convert String Input from Prompt Boxes to Integers in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!