Javascript (+) sign concatenates instead of giving

2019-01-01 03:41发布

Why when I use this: (assuming i = 1)

divID = "question-" + i+1;

I get question-11 and not question-2?

13条回答
忆尘夕之涩
2楼-- · 2019-01-01 04:34

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.

divID = "question-" + (i+1);
查看更多
旧时光的记忆
3楼-- · 2019-01-01 04:34

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.

查看更多
无与为乐者.
4楼-- · 2019-01-01 04:35

using braces surrounding the numbers will treat as addition instead of concat.

divID = "question-" + (i+1)
查看更多
无与为乐者.
5楼-- · 2019-01-01 04:36

Another alternative could be using:

divID = "question-" + i- -1;

Subtracting a negative is the same as adding, and a minus cannot be used for concatenation

查看更多
美炸的是我
6楼-- · 2019-01-01 04:40

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

You can make it work by using Number()

arg1 = Number($("#elemId").val());
arg2 = 1;

someFuntion(arg1 + arg2);

or

arg1 = $("#elemId").val();
arg2 = 1;

someFuntion(Number(arg1) + arg2);
查看更多
浪荡孟婆
7楼-- · 2019-01-01 04:41

Use only:

divID = "question-" + parseInt(i) + 1;

When "n" comes from html input field or is declared as string, you need to use explicit conversion.

var n = "1"; //type is string
var frstCol = 5;
lstCol = frstCol + parseInt(n);

If "n" is integer, don't need conversion.

n = 1; //type is int
var frstCol = 5, lstCol = frstCol + n;
查看更多
登录 后发表回答