Javascript (+) 符号连接而不是给出变量的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5961000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:41:20  来源:igfitidea点击:

Javascript (+) sign concatenates instead of giving sum of variables

javascriptmath

提问by ilyo

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

为什么当我使用这个时:(假设i = 1

divID = "question-" + i+1;

I get question-11and not question-2?

我得到问题 11而不是问题 2

采纳答案by Joachim Sauer

Use this instead:

改用这个:

var divID = "question-" + (i+1)

It's a fairly common problem and doesn't just happen in JavaScript. The idea is that +can represent bothconcatenation and addition.

这是一个相当普遍的问题,不仅仅发生在 JavaScript 中。这个想法是,+可以代表两个串联和补充。

Since the + operator will be handled left-to-right the decisions in your code look like this:

由于 + 运算符将从左到右处理,因此您的代码中的决定如下所示:

  • "question-" + i: since "question-"is a string, we'll do concatenation, resulting in "question-1"
  • "question-1" + 1: since "queston-1"is a string, we'll do concatenation, resulting in "question-11".
  • "question-" + i: 由于"question-"是一个字符串,我们将进行连接,导致"question-1"
  • "question-1" + 1: 由于"queston-1"是一个字符串,我们将进行连接,结果是"question-11".

With "question-" + (i+1)it's different:

随着"question-" + (i+1)它的不同:

  • since the (i+1)is in parenthesis, its value must be calculated before the first +can be applied:
    • iis numeric, 1is numeric, so we'll do addition, resulting in 2
  • "question-" + 2: since "question-"is a string, we'll do concatenation, resulting in "question-2".
  • 由于(i+1)括号中的是 ,因此必须+在应用第一个之前计算其值:
    • i是数字,1是数字,所以我们要做加法,结果是2
  • "question-" + 2: 由于"question-"是一个字符串,我们将进行连接,结果是"question-2".

回答by Serafeim

You may also use this

你也可以使用这个

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

to be sure that iis converted to integer.

以确保i转换为整数。

回答by edercortes

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.

当 "n" 来自 html 输入字段或声明为字符串时,您需要使用显式转换。

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

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

如果“n”是整数,则不需要转换。

n = 1; //type is int
var frstCol = 5, lstCol = frstCol + n;

回答by Tim Hobbs

Since you are concatenating numbers on to a string, the whole thing is treated as a string. When you want to add numbers together, you either need to do it separately and assign it to a var and use that var, like this:

由于您将数字连接到字符串上,因此整个内容都被视为字符串。当您想将数字相加时,您需要单独进行并将其分配给一个 var 并使用该 var,如下所示:

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

Or you need to specify the number addition like this:

或者您需要像这样指定数字加法:

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

EDIT

编辑

I should have added this long ago, but based on the comments, this works as well:

我早就应该添加这个,但根据评论,这也有效:

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

回答by rsplak

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

check it here, it's a JSFiddle

在这里检查,这是一个 JSFiddle

回答by musafar006

Joachim Sauer's answerwill work in scenarios like this. But there are some instances where adding brackets won't help.

Joachim Sauer 的回答将适用于这样的场景。但在某些情况下,添加括号无济于事。

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()

您可以使用 Number()

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

someFuntion(arg1 + arg2);

or

或者

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

someFuntion(Number(arg1) + arg2);

回答by Billy Moon

Add brackets

添加括号

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

回答by niksvp

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

在数字周围使用大括号将视为加法而不是连接。

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

回答by Jamiec

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 iis happening first giving the string "question=1". Then another string concatenation with "1" giving "question-11".

在您的情况下,“问题-”的连接i首先发生,给出字符串“问题= 1”。然后另一个字符串连接“1”给出“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);

回答by Avanish Kumar

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

Use this +operator behave as concatthat's why it showing 11.

使用此+运算符的行为就像concat这就是它显示 11 的原因。