how to add two numbers in javascript?

Today, We want to share with you add two numbers in javascript.In this post we will show you JavaScript Variables and Constants, hear for JavaScript Operators we will give you demo and example for implement.In this post, we will learn about How To Added 2 Numbers In Javascript Using Textbox? with an example.

JavaScript Program to Add Two Numbers

here i learn to Adding 2 numbers concatenates them instead of calculating the sum.

Find Sum of 2 Numbers Using JavaScript

<!doctype html>
function add(){
var a,b,c;
c= a + b;
document.getElementById("results").value= c;
Enter the First number : <input id="primary">
Enter the Second number: <input id="secondary">
<button onclick="add()">Added</button>
<input id="results">

Example 1: Add 2 Numbers

const primary = 5;
const secondary = 3;

// add 2 numbers
const total = primary + secondary;

// display the total
console.log('The total of ' + primary + ' and ' + secondary + ' is: ' + total);


The total of 5 and 3 is: 8

Example 2: Add 2 Numbers Entered by the User


// store input numbers
const primary = parseInt(prompt('Enter the primary number '));
const secondary = parseInt(prompt('Enter the secondary number '));

//add 2 numbers
const total = primary + secondary;

// display the total
console.log(`The total of ${primary} and ${secondary} is ${total}`);


Enter the primary number 5
Enter the secondary number 3
The total of 5 and 3 is: 8

javascript how to sum 2 numbers

how to add 2 numbers in javascript

let a = 4;
let b = 5;
document.write(a + b);

javascript function to add 2 numbers

<!--Added 2 integer numbers using JavaScript.-->
		<title>Added 2 integer numbers using JavaScript. -</title>
		<script type="text/javascript">
			function addTwoNumbers(textBox1, textBox2){
				var x=document.getElementById(textBox1).value;
				var y=document.getElementById(textBox2).value;
				var total=0;
				alert("SUM is: " + total);
	<h2>Added 2 integer numbers using JavaScript.</h2>
	<b>Enter primary Number: </b><br>
	<input type="text" id="userData1"/><br>
	<b>Enter secondary Number: </b><br>
	<input type="text" id="userData2"/><br><br>
	<input type="button" id="btnSum" value="Calculate SUM" onClick="addTwoNumbers('userData1','userData2')"/>


Added 2 numbers javascript

var x = 2 + 3;
//x = 5

JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic on numbers:

Read Also:  Form Input Bindings in vue.js
Operator Description
+ Addition
* Multiplication
** Exponentiation (ES2016)
/ Division
% Modulus (Division Remainder)
++ Increment

I hope you get an idea about javascript sum of numbers.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  install and/or enable a php accelerator (highly recommended)
  2. Read Also:  Laravel 6 Image Uploader and Browser for CKEditor
  3. Read Also:  Angular 6 SEO (Search Engine Optimization) Tutorial
  4. Read Also:  How to Create Model In Laravel 6 Example?
  5. Read Also:  Vue router Get Current URL in Vue.js Example
  6. Read Also:  jQuery Check an Element is Visible or not
  7. Read Also:  Laravel Advance Database Query Builder
  8. Read Also:  How to get the sum of numbers in an array javascript?
  9. Read Also:  Simple Angular 6 Observables Example
  10. Read Also:  Elementor cpt select Integration