Howdy readers, today you will learn how to write a program to add two numbers using JavaScript Programming language.

We will use the following methods to add two numbers:
- Using Standard Method
- Using User Input
So, without any delay, let’s begin this tutorial.
JavaScript Program to Add Two Numbers
JavaScript Program
// JavaScript Program to Add Two Numbers var num1 = 5; var num2 = 7; // Adding two numbers var sum = num1 + num2; // Displaying sum console.log("The sum of " + num1 + " and " + num2 + " is: " + sum);
Output
The sum of 5 and 7 is: 12
Explanation
var num1 = 5; var num2 = 7;
We have defined two variables with static integer values.
// Adding two numbers var sum = num1 + num2;
Then, we add those two variables using the (+) operator and store the result in the third variable named sum
.
// Displaying sum console.log("The sum of " + num1 + " and " + num2 + " is: " + sum);
Finally, the result is displayed on the screen using the console.log()
method.
JavaScript Program to Add Two Numbers Using User Input
JavaScript Program
// JavaScript Program to Add Two Numbers With User Input var first = parseInt(prompt("Enter the first number: ")); var second = parseInt(prompt("Enter the second number: ")); // Add two numbers var sum = first + second; // Displaying result console.log("The sum of two numbers is: " + sum);
Output
Enter the first number: 18 Enter the second number: 24 The sum of two numbers is: 42
Explanation
var first = parseInt(prompt("Enter the first number: ")); var second = parseInt(prompt("Enter the second number: "));
The program prompts the user to enter two numbers. The prompt()
is used to take input from the user and parseInt()
is used to convert the user input string to a number.
// Add two numbers var sum = first + second;
Then, we add those two numbers simply by using the (+) operator.
// Displaying result console.log("The sum of two numbers is: " + sum);
And the sum of two numbers is displayed on the screen using the console.log()
method.
Conclusion
I hope after reading this tutorial, you understand how to write a program to add two numbers using JavaScript Programming language.
If you have any queries regarding the tutorial, then let us know in the comment section. We will be pleased to solve all of your queries.