Codeforcoding

JavaScript Program to Add Two Numbers

JavaScript Program to Add Two Numbers

In this tutorial, we will discuss the title of the JavaScript Program to Add Two Numbers

In this topic, we are going to learn how  to write a program to add two numbers in the JavaScript programming language

JavaScript Program to Add Two Numbers
Addition

Sum of two numbers

JavaScript Sum of two numbers

Program 1

Here, We use the plus(+) operator to find the sum of two numbers. The addition of two numbers 25 and 50 and the output is displayed here

const num_1=15;
const num_2=12;
//variable declaration

//Addition of two numbers
const sum=num_1+num_2;

//display the result o sum
console.log('The addition of '+num_1+' and '+num_2+' is: '+sum);

When the above code is executed, it produces the following result

The addition of 15 and 12 is: 27

 

JavaScript sum of two numbers using HTML

Program 2

Here, We use the plus(+) operator to find the sum of two numbers. The addition of two numbers 10 and 20 and the output is displayed on a web page using HTML

<html>
<head>
<title>Add two numbers in Java script</title>
<script>
var fNum=10;
var lNum=20;
var sum=fNum+lNum;
document.write("Sum: "+sum);
</script>
</head>
<body>

</body>
</html>

When the above code is executed, it produces the following result

Sum: 30

 

JavaScript Add two numbers using the form

Program 3

The program asks the user to enter two numbers using the form. then the result of the sum of two numbers is displayed when the user clicks the “add” button

<html>
<head>
<title>Add two numbers in Java script</title>
<script>
function add()//function definition
{
var fNum,sNum,sum;
fNum=parseInt(document.getElementById("first").value);
sNum=parseInt(document.getElementById("second").value);
sum=fNum+sNum;
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()">Add</button>
//call the function when click 'add' button
<p> sum =<input id="ans"></p>
</body>
</html>

When the above code is executed, it produces the following result

 

Output2

 

Similar post

Sum of two numbers in C language

Sum of two numbers in C++ language

Sum of two numbers in Python language

Sum of two numbers in Java language without + operator

Sum of two numbers in Java language using bitwise operator

Sum of two numbers in C language without + operator

Sum of two numbers in C language using bitwise operator

 

Calculate the addition of two numbers using the method in Java language

Calculate the addition of two numbers using a function in C language

Calculate the addition of two numbers using a function in C++ language

Calculate the addition of two numbers using a function in Python language

 

PHP program to Addition of Two Numbers using function - PHP program
5 methods to add two numbers in Java
Exit mobile version