How to input two numbers and sum them in javascript

青灯夜游
Release: 2022-09-20 16:02:54
Original
8446 people have browsed it

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".

How to input two numbers and sum them in javascript

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;
Copy after login

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;
Copy after login

Step 3: Use the " " operator to add the two numbers entered by the user

num1+ num2
Copy after login

Implementation code:

var num1 = prompt('请输入第一个数字') * 1;
var num2 = prompt('请输入第二个数字') * 1;
console.log("两个数字相加的和为:"+(num1 + num2));
Copy after login

How to input two numbers and sum them in javascript

【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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template