# 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.

Free Live Chat for Any Issue

Find Sum of 2 Numbers Using JavaScript

```<!doctype html>
<html>
<script>
var a,b,c;
a=Number(document.getElementById("primary").value);
b=Number(document.getElementById("secondary").value);
c= a + b;
document.getElementById("results").value= c;
}
</script>
<body>
Enter the First number : <input id="primary">
Enter the Second number: <input id="secondary">
<input id="results">
</body>
</html>
```

### Example 1: Add 2 Numbers

```const primary = 5;
const secondary = 3;

const total = primary + secondary;

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

Results

```The total of 5 and 3 is: 8
```

### Example 2: Add 2 Numbers Entered by the User

Code

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

const total = primary + secondary;

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

Results

```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.-->
<html>
<title>Added 2 integer numbers using JavaScript. - www.pakainfo.com</title>
<script type="text/javascript">
var x=document.getElementById(textBox1).value;
var y=document.getElementById(textBox2).value;
var total=0;
total=Number(x)+Number(y);
}
</script>
<body>
<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')"/>
</body>

</html>

```

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

### JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic on numbers:

Read Also:  Laravel 7 jQuery Ajax Request Tutorial
Operator Description