Welcome folks today in this blog post we will be building a program to calculate sum of two numbers which are provided by users and adding them
in javascript. All the full source code of the application is given below.
Get Started
In order to get started create a index.html
file and copy paste the code which is shown below
First of all we will be adding
two numbers which are static in application
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html> <head> <title>Add Two Numbers in Javascript</title> </head> <body> </body> <script> const num1 = 5; const num2 = 3; // add two numbers const sum = num1 + num2; // display the sum console.log('The sum of ' + num1 + ' and ' + num2 + ' is: ' + sum); </script> </html> |
Now if you open index.html
file inside the browser you will see the below result
User Input of Numbers Example
Now we will be taking the two numbers as input
by the user
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html> <head> <title>Add Two Numbers in Javascript</title> </head> <body> </body> <script> // store input numbers const num1 = parseInt(prompt('Enter the first number ')); const num2 = parseInt(prompt('Enter the second number ')); //add two numbers const sum = num1 + num2; alert(`The sum of two numbers is: ${sum}`) </script> </html> |