Implementation steps: 1. Define two variables to store the numbers entered by the user, with the syntax "var num1,num2;"; 2. Use the prompt() function to obtain the numbers entered by the user and assign them to the two Variable, syntax "num1 = prompt('Please enter the first number') * 1; num2 = prompt('Please enter the second number') * 1;"; 3. Use the " " operator to combine the two values entered by the user. To add up numbers, the syntax is "num1 num2".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In javascript, you can use the prompt() function and the " " operator to input two numbers and sum them.
Implementation steps:
Step 1: Define two variables to store the numbers entered by the user
var num1,num2;
Step 2: Use the prompt() function to obtain the number entered by the user and assign it to two variables
The prompt() method is used to display a dialog box that prompts the user for input. The function return value is a string.
When the user input is a number or Boolean type, string type will always be returned. If you want to keep the type unchanged, you need to perform type conversion.
num1 = prompt('请输入第一个数字') * 1; num2 = prompt('请输入第二个数字') * 1;
Step 3: Use the " " operator to add the two numbers entered by the user
num1+ num2
Implementation code:
var num1 = prompt('请输入第一个数字') * 1; var num2 = prompt('请输入第二个数字') * 1; console.log("两个数字相加的和为:"+(num1 + num2));
【Related recommendations: javascript video tutorial, Basic programming video】
The above is the detailed content of How to input two numbers and sum them in javascript. For more information, please follow other related articles on the PHP Chinese website!