5

I created a simple program that make the sum of two numbers BUT.. the program is concatenating instead, This is so confusing! Can anyone help?

function calculate() {
  var numberOne = document.querySelector(".first").value;
  var numberTwo = document.querySelector(".second").value;
  var sum = numberOne + numberTwo;
  document.querySelector(".result").innerHTML = "The sum of the two numbers is : " + sum;
}
<!doctype html>
<html>

<body>
  <p>Calculate sum of two numbers !</p>
  Enter 1rst Number:<br>
  <input type="number" class="first" placeholder=""><br><br> Enter 2nd Number:<br>
  <input type="number" class="second" placeholder=""><br><br>
  <input type="button" onclick="calculate()" value="calculate">
  <p class="result"></p>
</body>

</html>
Edwin
  • 1,932
  • 16
  • 26
Elight7
  • 81
  • 1
  • 4
  • look at this answer +num1 + +num2; -> https://stackoverflow.com/a/8976659/5323912 – N1gthm4r3 Aug 23 '17 at 13:02
  • 1
    Possible duplicate of [Adding two numbers concatenates them instead of calculating the sum](https://stackoverflow.com/questions/14496531/adding-two-numbers-concatenates-them-instead-of-calculating-the-sum) – JJJ Aug 23 '17 at 13:06

7 Answers7

16

Here value gives you a string, hence the concatenation. Try parsing it as an Number instead:

var sum = parseInt(numberOne) + parseInt(numberTwo);

See the demo fiddle.

cнŝdk
  • 30,215
  • 7
  • 54
  • 72
Suresh Atta
  • 118,038
  • 37
  • 189
  • 297
3

Javascript takes dom elements as string only by default. To make mathematical calculation, you need to typecast it to integer/float or convert to number.

parseInt(number) = number, truncates after decimal value
parseFloat(number) = number with decimal values
Number(number) = number with or without decimal
Aman Kumar
  • 232
  • 4
  • 11
2

Your numberOne and numberTwo are strings, so you get concatenated strings when use + sign with two string. Parse first to numbers then sum them. You can use parseInt() and parseFloat() functions for it.

var numberOne = '7';
var numberTwo = '8';

var sum = numberOne + numberTwo;
console.log(sum);

sum = parseFloat(numberOne) + parseFloat(numberTwo);
console.log(sum);
Suren Srapyan
  • 62,337
  • 13
  • 111
  • 105
1

Use parseInt() for this, Check snippet below

function calculate() {
    var numberOne = document.querySelector(".first").value;
    var numberTwo = document.querySelector(".second").value;
    var sum = parseInt(numberOne) + parseInt(numberTwo);
    document.querySelector(".result").innerHTML = "The sum of the two numbers is : " + sum;
  }
 <p>Calculate sum of two numbers !</p>
  Enter 1rst Number:<br>
  <input type="number" class="first" placeholder=""><br><br>
  Enter 2nd Number:<br>
  <input type="number" class="second" placeholder=""><br><br>
  <input type="button" onclick="calculate()" value="calculate">
  <p class="result"></p>
Super User
  • 9,078
  • 3
  • 27
  • 46
1

Example , you can use this:

var numberOne:number = +document.querySelector(".first").value;
var numberTwo:number = +document.querySelector(".second").value;
var sum = numberOne + numberTwo;

You should use + Angular typeScript

Mohnish
  • 1,006
  • 1
  • 11
  • 19
  • Worked for me. parseInt did not. But then I was fixing a concatenation issue regarding my image slide indexes in an image modal/slider. – Maria Campbell May 01 '21 at 09:57
0

It's just because value returns with string. So sum operation ends with concatenation. you need to convert both those values.

user below code

 var sum = parseFloat(numberOne) + parseFloat(numberTwo);
Mr. Go
  • 567
  • 1
  • 5
  • 20
0

Who are using javascript in salesforce make sure

var a= 8 ;
var b =8 ;
var c = a+b;

This will give u result output = 88; It is will just concatenate. If you want to add those two: You should write logic as :

var a = 8;
var b = 8
var c = parseInt(a)+ parseInt(b);

This will give you the desired result of 16 , The same is the case with multiplication.

Hope this helps.

jizhihaoSAMA
  • 11,804
  • 9
  • 23
  • 43