WAP in JavaScript to accept two numbers and perform addition of two numbers by using mouseover event

Add Two Numbers in JavaScript

Here is the simplest JavaScript code to add two numbers.

var numOne = 10;
var numTwo = 20;
var sum = numOne+numTwo;

As you can see from the above three lines of JavaScript code. The value 10 gets initialized to numOne. So numOne=10. And the value 20 gets initialized to numTwo. So numTwo=20. Now the statement, numOne+numTwo or 10+20 or 30 gets initialized to sum. So sum=30, which is the addition of two numbers stored in numOne and numTwo

On same concept as mentioned above, I’m going to create another JavaScript code that also adds two numbers and displays the result as an HTML output.

JavaScript Add Two Numbers using Form and TextBox

This is the actual program to add two numbers in JavaScript using form and TextBoxes. As this program allows users to input the data. On the basis of user input, JavaScript code gets in action to output the addition result

<!doctype html>
<html>
<head>
<script>
function add()
{
  var numOne, numTwo, sum;
  numOne = parseInt(document.getElementById("first").value);
  numTwo = parseInt(document.getElementById("second").value);
  sum = numOne + numTwo;
  document.getElementById("answer").value = sum;
}
</script>
</head>
<body>

<p>Enter the First Number: <input id="first"></p>
<p>Enter the Second Number: <input id="second"></p>
<button onclick="add()">Add</button>
<p>Sum = <input id="answer"></p>

</body>
</html>

Leave a Comment

Scroll to Top