Javascript concatenar variables y string


When concatenating a string with the variables, the operations are not performed well. When the addition is done, by putting the string "Sum", the variables are concatenated, on the other hand if I remove them, they add well.

function mates(){
    var result = document.getElementById("result");
  var num1 = parseInt(prompt("Numero 1"));
  var num2 = parseInt(prompt("Numero 2"));
  result.innerHTML = "Suma"+num1 + num2+ "<br />";
  result.innerHTML += "Resta" +num1 - num2;

      <input id="bot1" type="button" value="F1" onclick="mates()" />
    <p id="result"></p>


Have you tried putting parentheses?

result.innerHTML = "Suma" + (num1 + num2) + "<br />";
result.innerHTML += "Resta" + (num1 - num2);

With the parentheses you indicate the priority of the operations.

Scroll to Top