0

Please don't mark this as duplicate. It's similar to the following , but not the same.

I have looked at:

Javascript concatenating numbers, not adding up and: How to add two strings as if they were numbers? and: innerHTML return NaN with text

Here is my code:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>My Web Page</title>


<form action="PayslipServlet" method="get">
          First Number:    <input type="text" name="n1" id="n1"><br/>
          Second Number:   <input type="text" name="n2" id="n2"><br/>
          Sum: <span id="Sum"></span>
          <br>
          <input type="button" value="Submit" onClick="pr()">
        </form>

        <script>
          function pr() {
            var foobar = 100;
            <!-- works -->
            <!-- document.getElementById("Sum").innerHTML = +document.getElementById('n1').value  ; works -->
            <!-- works -->
            <!-- document.getElementById("Sum").innerHTML = +document.getElementById('n2').value  ; works -->
            <!-- works -->
            <!-- document.getElementById("Sum").innerHTML = +document.getElementById('n1').value +foobar  ; -->
            <!-- concatenates as strings: why? -->
            document.getElementById("Sum").innerHTML = +document.getElementById('n1').value +document.getElementById('n2').value  ;
          }
        </script>



</head>


The problem statement:

When I feed n1 back to sum, works (please see the commented lines) When I feed n2 back to sum, works When I feed n1+foobar to sum, works

BUT, when I feed n1+n2 back to sum, I get a concatenated string (e.g. 1+2 becomes 12, not 3).

What am I doing wrong?

Thanks!

Mamun
  • 2,322
  • 4
  • 27
  • 41

3 Answers3

1

You need to convert both values to numbers before adding them, so you'll get addition rather than concatenation.

document.getElementById("Sum").innerHTML = +document.getElementById('n1').value + +document.getElementById('n2').value  ;
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You need to use the parseInt directive to convert the input values to int data type. Here is the code:

    <script>
      function pr() {
        var foobar = 100;
        <!-- works -->
        <!-- document.getElementById("Sum").innerHTML = +document.getElementById('n1').value  ; works -->
        <!-- works -->
        <!-- document.getElementById("Sum").innerHTML = +document.getElementById('n2').value  ; works -->
        <!-- works -->
        <!-- document.getElementById("Sum").innerHTML = +document.getElementById('n1').value +foobar  ; -->
        <!-- concatenates as strings: why? -->
        document.getElementById("Sum").innerHTML = parseInt(document.getElementById('n1').value) +parseInt(document.getElementById('n2').value)  ;
      }
    </script>
Angela
  • 27
  • 6
1

You're using two different kinds of +s there. With the first two:

<!-- works -->
<!-- document.getElementById("Sum").innerHTML = +document.getElementById('n1').value  ; works -->
<!-- works -->
<!-- document.getElementById("Sum").innerHTML = +document.getElementById('n2').value  ; works -->

the operation performed is leftSide = +expression

This is the unary plus operator, which coerces non-numbers to numbers.

In the third:

<!-- works -->
<!-- document.getElementById("Sum").innerHTML = +document.getElementById('n1').value +foobar  ; -->

the operation performed is leftSide = +expression1 + expression2

Unary plus is used on expression1 to turn it into a number. But the + between expression1 and expression2 is not unary plus, but addition/concatenation. When + is used between two expressions, if both are numeric, they get added together. Here, after unary plus on the .value, it's a number, and foobar is a number too, so they get added together.

But in

document.getElementById("Sum").innerHTML = +document.getElementById('n1').value +document.getElementById('n2').value

Now, the right expression is a string not a number. + only adds when both sides are numbers. someNumber + someString results in concatenation.

If you want to add, make sure both sides of the + are numbers first.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320