javascript (+) sign concatenates instead of giving sum?

后端 未结 7 527
旧巷少年郎
旧巷少年郎 2020-12-07 03:41

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?

7条回答
  •  孤城傲影
    2020-12-07 04:18

    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;
      }
     

    Calculate sum of two numbers !

    Enter 1rst Number:


    Enter 2nd Number:


提交回复
热议问题