I've been trying to solve this problem for the last couple days: when I subtract, multiply or divide 2 numbers input through a prompt, everything works fine; but when I want to add them, I get the 2 numbers simply written together.
Example: if I add 5 and 6, I get 56!!
Here's the code I'm working with.
var a = prompt("Enter first number");
var b = prompt("Enter second number");
alert(a + b);
What am I doing wrong? Do I have to specify the value type?
The function prompt returns a string and + is (unwisely, perhaps) used for both string concatenation and number addition.
You do not "specify types" in JavaScript but you can do string to number conversion at run time. There are many ways to so this. The simplest is:
var a = +prompt("Enter first number");
var b = +prompt("Enter second number");
alert(a + b);
but you can also do
var a = Number(prompt("Enter first number"));
var b = Number(prompt("Enter second number"));
alert(a + b);
(Avoid parseInt because it only handles the leading characters and will not add numbers like 4.5 and 2.6.)
The problem is that JavaScript is loosely typed. So, it doesn't know that "5" is a number, actually, because everything you receive from a prompt is String.
You can do:
var a = prompt("1st") * 1;
var b = prompt("2nd") * 1;
alert (a + b);
This works because the * operator forces everything to act like numbers.
You are using concatination operation instead of arithmetic operator.
In your above code:
alert(a + b);
Your browser compiler will assume it as concatination. So it concatinating two strings. That is why you are getting that output.
When you enter var a and var b, the variables are probably set strings(characters) and not as integers.
So, when you use a + b, your'e putting those characters together.
For ensuring that the value you enter saves as an integer, you can you use the
parseInt()
For example: var a = parseInt( prompt("Enter first number") );
try this ,
var a = prompt("Enter first number");
var b = prompt("Enter second number");
var x=parseInt(a);
var y=parseInt(b);
alert(x+y);
var a = parseInt(prompt("Enter first number"));
var b = parseInt(prompt("Enter second number"));
alert(a + b);
You use the parseInt before the prompt because its will take as input as string if you use the parseInt it will take as the number.
1) The function prompt returns a string and '+' is (unwisely, perhaps) used for both string concatenation and number addition.
var a = +prompt("Enter first number");
var b = +prompt("Enter second number");
alert(a + b);
2.)some developers use parseIntbefore prompt ,But its not good way because that not add floating numbers like 5.2 ,9.99 etc
var a = parseInt(prompt("Enter first number"));
var b = parseInt(prompt("Enter second number"));
alert(a + b);
BEST WAY
You can do with other different method for add number in prompt box put Number before prompt.
var a = Number(prompt("Enter first number"));
var b = Number(prompt("Enter second number"));
alert(a + b);
JavaScript is loosely coupled.
<script>
var a = prompt("Enter first number","");
var b = prompt("Enter second number","");
a=Number(a);
b=Number(b);
alert(a+b);
</script>
This will also work converting a String to Number and alert.
来源:https://stackoverflow.com/questions/22704963/sum-of-two-numbers-with-prompt