The reason you get that is the order of precendence of the operators, and the fact that + is used to both concatenate strings as well as perform numeric addition.
In your case, the concatenation of "question-" and i is happening first giving the string "question=1". Then another string concatenation with "1" giving "question-11".
You just simply need to give the interpreter a hint as to what order of prec endence you want.
One place the parentheses suggestion fails is if say both numbers are HTML input variables.
Say a and b are variables and one receives their values as follows (I am no HTML expert but my son ran into this and there was no parentheses solution i.e.
HTML inputs were intended numerical values for variables a and b, so say the inputs were 2 and 3.
Following gave string concatenation outputs: a+b displayed 23; +a+b displayed 23; (a)+(b) displayed 23;
From suggestions above we tried successfully : Number(a)+Number(b) displayed 5; parseInt(a) + parseInt(b) displayed 5.
Thanks for the help just an FYI - was very confusing and I his Dad got yelled at 'that is was Blogger.com's fault" - no it's a feature of HTML input default combined with the 'addition' operator, when they occur together, the default left-justified interpretation of all and any input variable is that of a string, and hence the addition operator acts naturally in its dual / parallel role now as a concatenation operator since as you folks explained above it is left-justification type of interpretation protocol in Java and Java script thereafter. Very interesting fact. You folks offered up the solution, I am adding the detail for others who run into this.
Joachim Sauer's answer will work in scenarios like this. But there are some instances where adding brackets won't help.
For eg: You are passing 'sum of value of an input element and an integer' as an argument to a function.
arg1 = $("#elemId").val(); // value is treated as string
arg2 = 1;
someFuntion(arg1 + arg2); // and so the values are merged here
someFuntion((arg1 + arg2)); // and here
The reason you get that is the order of precendence of the operators, and the fact that
+
is used to both concatenate strings as well as perform numeric addition.In your case, the concatenation of "question-" and
i
is happening first giving the string "question=1". Then another string concatenation with "1" giving "question-11".You just simply need to give the interpreter a hint as to what order of prec endence you want.
One place the parentheses suggestion fails is if say both numbers are HTML input variables. Say a and b are variables and one receives their values as follows (I am no HTML expert but my son ran into this and there was no parentheses solution i.e.
Thanks for the help just an FYI - was very confusing and I his Dad got yelled at 'that is was Blogger.com's fault" - no it's a feature of HTML input default combined with the 'addition' operator, when they occur together, the default left-justified interpretation of all and any input variable is that of a string, and hence the addition operator acts naturally in its dual / parallel role now as a concatenation operator since as you folks explained above it is left-justification type of interpretation protocol in Java and Java script thereafter. Very interesting fact. You folks offered up the solution, I am adding the detail for others who run into this.
using braces surrounding the numbers will treat as addition instead of concat.
Another alternative could be using:
Subtracting a negative is the same as adding, and a minus cannot be used for concatenation
Joachim Sauer's answer will work in scenarios like this. But there are some instances where adding brackets won't help.
For eg: You are passing 'sum of value of an input element and an integer' as an argument to a function.
You can make it work by using
Number()
or
Use only:
When "n" comes from html input field or is declared as string, you need to use explicit conversion.
If "n" is integer, don't need conversion.