The parseInt() Method
In the example below the user is prompted to enter two numbers which are then multiplied.
var No1 = prompt("Enter the first number:", "");
var No2 = prompt("Enter the second number:", "");
var Product = No1 * No2;
alert(Product);
As long as numeric characters are entered, the correct answer is produced. However, this example would not work correctly if the two numbers are added: No1 + No2. The reason is that data entered into prompt boxes are treated as strings. As long as multiplication (or division or subtraction) is applied to the values, JavaScript converts the strings to numbers and applies the arithmetic operator. For addition, though, the "+" operator is interpreted as concatenation because both entered values are strings. This interpretation is not peculiar to prompt boxes. All methods of user input treat the entered data as strings. Therefore, it is usually necessary to explicitly convert entered data to numeric values when addition is to be performed.
JavaScript provides two methods to convert character strings to numbers. First, the parseInt() method converts a string containing numeric characters to an integer value.
parseInt(string)
Working addition example:
var No1 = prompt("Enter the first number:", "");
var No2 = prompt("Enter the second number:", "");
var Product = parseInt(No1) + parseInt(No2);
alert(Product);


