Addition of two numbers

Addition of two numbersUsing JavaScript statement we can perform arithmetic operations like +, -, *, /

E.g.

<html> 
<head> 
<title>Adding two numbers</title> 
</head> 
<body>
<center>
Add two Numbers
<form name="form1">  
Enter First Number: <input type="text" name="val1">  
Enter Second Number: <input type="text" name="val2">
<input type="button" onclick="valadd()" value="Add">  
</form>  
<script language="Javascript">
function valadd() 
{
var a=document.form1.val1.value;
var b=document.form1.val2.value;
if(a.length==0 || b.length==0)
{
alert("Please enter all required values")
}
else if(isNaN(a) || isNaN(b))
{
alert("Only number/s allowed in both fields")
}
else
{
var c= eval(a)+ eval(b); 
alert("Result is: " + c + "\nIf you like this post then please \nshare it using share buttons \ngiven below");  
} 
} 
</script>
</center>
</body> 
</html>
Check Output: Adding two numbers Add two Numbers
Enter First Number: Enter Second Number:


Related Topics:
  1. JavaScript Introduction
  2. Variables in JavaScript
  3. JavaScript Function
  4. Accessing JavaScript Function
  5. Creating JavaScript interactive form
  6. Accessing form field value
  7. Creating interactive form login webpage
  8. Networking Fundamentals
  9. TCP/IP and Internet addressing
  10. Internet Working
  11. Internet Working Servers
  12. Concept and need for web publishing
  13. Web languages
  14. Javascript Control Structure
  15. Checking Text is Letter/s
  16. Checking Text is Number/s
  17. Checking Textbox is Empty or not
  18. Addition of two numbers
  19. Subtraction of two numbers
  20. Multiplication of two numbers
  21. Division of two numbers
  22. Form field validations
  23. Email validation