How can I add numbers instead of combining them in a string? [duplicate]










2
















This question already has an answer here:



  • Adding two numbers concatenates them instead of calculating the sum

    20 answers



If I use this code it makes from an number an string but I can't figure out why.



I want that this program adds 8+9 and makes = 17 not 89.



I try to learn this for as a hobby but I tried this almost a year ago and when I got stuck I never tried to make it work till now.






<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>

<script>

var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;

</script>

</body>
</html>












share|improve this question















marked as duplicate by isherwood, Graham, stealthyninja, Vivek Mishra, Gerhard Barnard Nov 14 '18 at 7:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 2





    Change your strings to numbers parseInt(a) + parseInt(b) should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Ariel Alvarado
    Nov 13 '18 at 22:46
















2
















This question already has an answer here:



  • Adding two numbers concatenates them instead of calculating the sum

    20 answers



If I use this code it makes from an number an string but I can't figure out why.



I want that this program adds 8+9 and makes = 17 not 89.



I try to learn this for as a hobby but I tried this almost a year ago and when I got stuck I never tried to make it work till now.






<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>

<script>

var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;

</script>

</body>
</html>












share|improve this question















marked as duplicate by isherwood, Graham, stealthyninja, Vivek Mishra, Gerhard Barnard Nov 14 '18 at 7:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 2





    Change your strings to numbers parseInt(a) + parseInt(b) should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Ariel Alvarado
    Nov 13 '18 at 22:46














2












2








2









This question already has an answer here:



  • Adding two numbers concatenates them instead of calculating the sum

    20 answers



If I use this code it makes from an number an string but I can't figure out why.



I want that this program adds 8+9 and makes = 17 not 89.



I try to learn this for as a hobby but I tried this almost a year ago and when I got stuck I never tried to make it work till now.






<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>

<script>

var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;

</script>

</body>
</html>












share|improve this question

















This question already has an answer here:



  • Adding two numbers concatenates them instead of calculating the sum

    20 answers



If I use this code it makes from an number an string but I can't figure out why.



I want that this program adds 8+9 and makes = 17 not 89.



I try to learn this for as a hobby but I tried this almost a year ago and when I got stuck I never tried to make it work till now.






<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>

<script>

var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;

</script>

</body>
</html>







This question already has an answer here:



  • Adding two numbers concatenates them instead of calculating the sum

    20 answers






<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>

<script>

var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;

</script>

</body>
</html>





<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>

<script>

var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;

</script>

</body>
</html>






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 3:05









isherwood

37k1082111




37k1082111










asked Nov 13 '18 at 22:43









GiovanniGiovanni

165




165




marked as duplicate by isherwood, Graham, stealthyninja, Vivek Mishra, Gerhard Barnard Nov 14 '18 at 7:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by isherwood, Graham, stealthyninja, Vivek Mishra, Gerhard Barnard Nov 14 '18 at 7:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 2





    Change your strings to numbers parseInt(a) + parseInt(b) should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Ariel Alvarado
    Nov 13 '18 at 22:46













  • 2





    Change your strings to numbers parseInt(a) + parseInt(b) should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Ariel Alvarado
    Nov 13 '18 at 22:46








2




2





Change your strings to numbers parseInt(a) + parseInt(b) should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

– Ariel Alvarado
Nov 13 '18 at 22:46






Change your strings to numbers parseInt(a) + parseInt(b) should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

– Ariel Alvarado
Nov 13 '18 at 22:46













3 Answers
3






active

oldest

votes


















1














document.getElementById("uitkomst1").innerHTML returns the string, so your result will be a string of two strings added together.



You need to convert the string to a number if you want to add two numbers. Javascript has different ways to do this.



In your case, it should use the Number class to convert. I think it is the best option because it can convert decimal numbers too.



var numberA = new Number(a); // "8.1"
var numberB = new Number(b); // "9.1"
var total = numberA + numberA ; // 17.2





share|improve this answer
































    3














    In JavaScript, you can use parseInt(string) to do that. Like,






    <!DOCTYPE html>
    <html>
    <body>

    <h2>JavaScript Variables</h2>

    <p id="demo_totaal"></p>
    <p id="uitkomst1">8</p>
    <p id="uitkomst2">9</p>

    <script>
    var a = document.getElementById("uitkomst1").innerHTML;
    var b = document.getElementById("uitkomst2").innerHTML;
    var total = parseInt(a) + parseInt(b);
    document.getElementById("demo_totaal").innerHTML = total;
    </script>
    </body>
    </html>








    share|improve this answer























    • Make sure user input don't have any decimals, else it will removed in parseInt

      – kiranvj
      Nov 14 '18 at 3:16


















    0














    There are different methods to do this using parseInt/parseFloat/new Number. See other answers for this.



    I use this mostly, multiply the number or string with 1 and then do addition. So far I havent seen any issues. Performance I am not sure though



    var total = a*1 + b*1;





    var a = "1";
    var b = "12.5";

    var total = a*1 + b*1;

    console.log(total)








    share|improve this answer





























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      document.getElementById("uitkomst1").innerHTML returns the string, so your result will be a string of two strings added together.



      You need to convert the string to a number if you want to add two numbers. Javascript has different ways to do this.



      In your case, it should use the Number class to convert. I think it is the best option because it can convert decimal numbers too.



      var numberA = new Number(a); // "8.1"
      var numberB = new Number(b); // "9.1"
      var total = numberA + numberA ; // 17.2





      share|improve this answer





























        1














        document.getElementById("uitkomst1").innerHTML returns the string, so your result will be a string of two strings added together.



        You need to convert the string to a number if you want to add two numbers. Javascript has different ways to do this.



        In your case, it should use the Number class to convert. I think it is the best option because it can convert decimal numbers too.



        var numberA = new Number(a); // "8.1"
        var numberB = new Number(b); // "9.1"
        var total = numberA + numberA ; // 17.2





        share|improve this answer



























          1












          1








          1







          document.getElementById("uitkomst1").innerHTML returns the string, so your result will be a string of two strings added together.



          You need to convert the string to a number if you want to add two numbers. Javascript has different ways to do this.



          In your case, it should use the Number class to convert. I think it is the best option because it can convert decimal numbers too.



          var numberA = new Number(a); // "8.1"
          var numberB = new Number(b); // "9.1"
          var total = numberA + numberA ; // 17.2





          share|improve this answer















          document.getElementById("uitkomst1").innerHTML returns the string, so your result will be a string of two strings added together.



          You need to convert the string to a number if you want to add two numbers. Javascript has different ways to do this.



          In your case, it should use the Number class to convert. I think it is the best option because it can convert decimal numbers too.



          var numberA = new Number(a); // "8.1"
          var numberB = new Number(b); // "9.1"
          var total = numberA + numberA ; // 17.2






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 4:18









          Mihai Chelaru

          2,210101122




          2,210101122










          answered Nov 14 '18 at 3:00









          BinBin

          262




          262























              3














              In JavaScript, you can use parseInt(string) to do that. Like,






              <!DOCTYPE html>
              <html>
              <body>

              <h2>JavaScript Variables</h2>

              <p id="demo_totaal"></p>
              <p id="uitkomst1">8</p>
              <p id="uitkomst2">9</p>

              <script>
              var a = document.getElementById("uitkomst1").innerHTML;
              var b = document.getElementById("uitkomst2").innerHTML;
              var total = parseInt(a) + parseInt(b);
              document.getElementById("demo_totaal").innerHTML = total;
              </script>
              </body>
              </html>








              share|improve this answer























              • Make sure user input don't have any decimals, else it will removed in parseInt

                – kiranvj
                Nov 14 '18 at 3:16















              3














              In JavaScript, you can use parseInt(string) to do that. Like,






              <!DOCTYPE html>
              <html>
              <body>

              <h2>JavaScript Variables</h2>

              <p id="demo_totaal"></p>
              <p id="uitkomst1">8</p>
              <p id="uitkomst2">9</p>

              <script>
              var a = document.getElementById("uitkomst1").innerHTML;
              var b = document.getElementById("uitkomst2").innerHTML;
              var total = parseInt(a) + parseInt(b);
              document.getElementById("demo_totaal").innerHTML = total;
              </script>
              </body>
              </html>








              share|improve this answer























              • Make sure user input don't have any decimals, else it will removed in parseInt

                – kiranvj
                Nov 14 '18 at 3:16













              3












              3








              3







              In JavaScript, you can use parseInt(string) to do that. Like,






              <!DOCTYPE html>
              <html>
              <body>

              <h2>JavaScript Variables</h2>

              <p id="demo_totaal"></p>
              <p id="uitkomst1">8</p>
              <p id="uitkomst2">9</p>

              <script>
              var a = document.getElementById("uitkomst1").innerHTML;
              var b = document.getElementById("uitkomst2").innerHTML;
              var total = parseInt(a) + parseInt(b);
              document.getElementById("demo_totaal").innerHTML = total;
              </script>
              </body>
              </html>








              share|improve this answer













              In JavaScript, you can use parseInt(string) to do that. Like,






              <!DOCTYPE html>
              <html>
              <body>

              <h2>JavaScript Variables</h2>

              <p id="demo_totaal"></p>
              <p id="uitkomst1">8</p>
              <p id="uitkomst2">9</p>

              <script>
              var a = document.getElementById("uitkomst1").innerHTML;
              var b = document.getElementById("uitkomst2").innerHTML;
              var total = parseInt(a) + parseInt(b);
              document.getElementById("demo_totaal").innerHTML = total;
              </script>
              </body>
              </html>








              <!DOCTYPE html>
              <html>
              <body>

              <h2>JavaScript Variables</h2>

              <p id="demo_totaal"></p>
              <p id="uitkomst1">8</p>
              <p id="uitkomst2">9</p>

              <script>
              var a = document.getElementById("uitkomst1").innerHTML;
              var b = document.getElementById("uitkomst2").innerHTML;
              var total = parseInt(a) + parseInt(b);
              document.getElementById("demo_totaal").innerHTML = total;
              </script>
              </body>
              </html>





              <!DOCTYPE html>
              <html>
              <body>

              <h2>JavaScript Variables</h2>

              <p id="demo_totaal"></p>
              <p id="uitkomst1">8</p>
              <p id="uitkomst2">9</p>

              <script>
              var a = document.getElementById("uitkomst1").innerHTML;
              var b = document.getElementById("uitkomst2").innerHTML;
              var total = parseInt(a) + parseInt(b);
              document.getElementById("demo_totaal").innerHTML = total;
              </script>
              </body>
              </html>






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 13 '18 at 22:47









              Elliott FrischElliott Frisch

              154k1391181




              154k1391181












              • Make sure user input don't have any decimals, else it will removed in parseInt

                – kiranvj
                Nov 14 '18 at 3:16

















              • Make sure user input don't have any decimals, else it will removed in parseInt

                – kiranvj
                Nov 14 '18 at 3:16
















              Make sure user input don't have any decimals, else it will removed in parseInt

              – kiranvj
              Nov 14 '18 at 3:16





              Make sure user input don't have any decimals, else it will removed in parseInt

              – kiranvj
              Nov 14 '18 at 3:16











              0














              There are different methods to do this using parseInt/parseFloat/new Number. See other answers for this.



              I use this mostly, multiply the number or string with 1 and then do addition. So far I havent seen any issues. Performance I am not sure though



              var total = a*1 + b*1;





              var a = "1";
              var b = "12.5";

              var total = a*1 + b*1;

              console.log(total)








              share|improve this answer



























                0














                There are different methods to do this using parseInt/parseFloat/new Number. See other answers for this.



                I use this mostly, multiply the number or string with 1 and then do addition. So far I havent seen any issues. Performance I am not sure though



                var total = a*1 + b*1;





                var a = "1";
                var b = "12.5";

                var total = a*1 + b*1;

                console.log(total)








                share|improve this answer

























                  0












                  0








                  0







                  There are different methods to do this using parseInt/parseFloat/new Number. See other answers for this.



                  I use this mostly, multiply the number or string with 1 and then do addition. So far I havent seen any issues. Performance I am not sure though



                  var total = a*1 + b*1;





                  var a = "1";
                  var b = "12.5";

                  var total = a*1 + b*1;

                  console.log(total)








                  share|improve this answer













                  There are different methods to do this using parseInt/parseFloat/new Number. See other answers for this.



                  I use this mostly, multiply the number or string with 1 and then do addition. So far I havent seen any issues. Performance I am not sure though



                  var total = a*1 + b*1;





                  var a = "1";
                  var b = "12.5";

                  var total = a*1 + b*1;

                  console.log(total)








                  var a = "1";
                  var b = "12.5";

                  var total = a*1 + b*1;

                  console.log(total)





                  var a = "1";
                  var b = "12.5";

                  var total = a*1 + b*1;

                  console.log(total)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 3:13









                  kiranvjkiranvj

                  12.7k23451




                  12.7k23451













                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3