0

I just want to add two numbers in javascript using prompt but the output comes as a string.

var num1 =prompt("enter a number");
var num2=prompt("enter a number");

var sum =num1+num2;

console.log(`the of ${num1}  and ${num2}  ${sum} `);
  • Prompt [returns a string](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt). Convert it if that's not what you want. – isherwood Oct 12 '20 at 15:41

1 Answers1

0

The return type of prompt is string so it is needed to convert string to number to do sum operation.

var num1 = Number(prompt("enter a number"));
var num2 = Number(prompt("enter a number"));

var sum =num1 + num2;

console.log(`the of ${num1}  and ${num2}  ${sum} `);
Derek Wang
  • 10,098
  • 4
  • 18
  • 39