I am beginner to javascript and i am getting unexpected output
here is the code
<script type="text/javascript">
function add(a,b)
{
x = a+b;
return x;
}
var num1 = prompt("what is your no.");
var num2 = prompt("what is another no.")
alert(add(num1,num2));
</script>
it should give output as a sum of two number entered by us on prompting but it is simply concatenating the two number and popping the output
This is because the
promptfunction returns aStringand not aNumber. So what you’re actually doing is to request 2 strings and then concatenate them. If you want to add the two numbers together you’ll have to convert the strings to numbers:or simpler: