Table of Contents
JavaScript Program for Adding Two Numbers | 4 different ways
In this article, we will discuss the concept of JavaScript Program for Adding Two Numbers
In this post, we are going to learn how to write a program to find addition of two numbers and dispay result in JS language
Code to Sum of two numbers in JS
Addition of two numbers in JS
In this code, we are going to learn how to write a program to addition of two numbers in JS programming language
In this program, the user initiates the values to variables and calculates the sum of the given numbers using plus(+) operator
Program 1
// JavaScript proram to addition of two numbers const f_Num=17; const l_Num=13; //declare and initaiize two variable //calculate sum of two numbers using '+' operator const sum=f_Num+l_Num; //display the sum of two numbers console.log('The addition of '+f_Num+' and '+l_Num+' is: '+sum);
When the above code is executed, it produces the following result
The addition of 17 and 13 is: 30
Addition of two numbers in JS using HTML
In this program, the user initiates the values to variables and calculates the sum of the given numbers using plus(+) operator inside the HTML tag
Program 2
<html> <head> <title>Sum of two numbers</title> <script> var f_Num=10; var l_Num=20; var add=f_Num+l_Num; document.write("The sum of "+f_Num+" and "+l_Num+" is: "+add); </script> </head> <body> </body> </html>
When the above code is executed, it produces the following result
The Sum of 10 and 20 is: 30
Addition of two numbers in JS using function – #1
In this program, the program is asked to enter two numbers from the user and calculates the sum of the given numbers using the function in JavaScript language
Program 3
<html> <head> <title>Sum of two numbers</title> <script> function add_Num()//define function for addition { var fNum,sNum,sum; f_Num=parseInt(document.getElementById("first").value); s_Num=parseInt(document.getElementById("second").value); sum=f_Num+s_Num; document.getElementById("ans").value=sum; } </script> </head> <body> <p>Enter the first value:<input id="first"></p> <p>Enter the second value:<input id="second"></p> <button onclick="add_Num()">Addition</button> <p> sum =<input id="ans"></p> </body> </html>
When the above code is executed, it produces the following result
Addition of two numbers in JS using function – #2
In this program, the program is asked to enter two numbers from the user and calculates the sum of the given numbers using the function in JavaScript language
Program 4
<html> <head> <title>Add two numbers</title> <script> function add()//function to addition { var f_Num,s_Num,sum; f_Num=Number(document.getElementById("first").value); s_Num=Number(document.getElementById("second").value); sum=f_Num+s_Num; document.getElementById("ans").innerHTML="Total:"+sum; } function clr()//function to clear the fields { document.getElementById("first").value=""; document.getElementById("second").value=""; document.getElementById("ans").innerHTML=""; } </script> </head> <body> <p>Enter the first value:<input type="text" id="first"></p> <p>Enter the second value:<input type="text" id="second"></p> <button onclick="add();">Add</button> <button onclick="clr();">clear</button> <p id="ans"></p> </body> </html>
When the above code is executed, it produces the following result
After addition
After clear
Suggested for you
Java program to add two numbers
C++ program to add two numbers
Python program to add two numbers
Write a program to sum of prime numbers 1 to n in Java language
Write a program to sum of prime numbers 1 to n in C language
Write a program to sum of prime numbers 1 to n in C++ language
Write a program to sum of prime numbers 1 to n in Python language
Write a program to sum of natural numbers 1 to n in Java language
Write a program to sum of prime numbers 1 to n in C language
Write a program to sum of prime numbers 1 to n in C++ language
Write a program to sum of prime numbers 1 to n in Python language