javascript i++ 与 ++i

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6867876/
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 23:33:08  来源:igfitidea点击:

javascript i++ vs ++i

javascriptoperators

提问by Web_Designer

In javascript I have seen i++used in many cases, and I understand that it adds one to the preceding value:

在 javascript 中,我已经看到i++在许多情况下使用过,并且我知道它会在前面的值上加 1:

for (var i=1; i<=10; i++) {
  console.log(i);
}

But what happens when I do this:

但是当我这样做时会发生什么:

++i;

And is it any different using the --operator (besides of course that it's subtraction rather than addition)?

使用--运算符有什么不同吗(当然,它是减法而不是加法)?

回答by Guffa

The difference between i++and ++iis the value of the expression.

i++和之间的区别++i是表达式的值。

The value i++is the value of ibefore the increment. The value of ++iis the value of iafter the increment.

该值i++i增量前的值。的值++ii增量后的值。

Example:

例子:

var i = 42;
alert(i++); // shows 42
alert(i); // shows 43
i = 42;
alert(++i); // shows 43
alert(i); // shows 43

The i--and --ioperators works the same way.

i----i运营商的工作方式相同。

回答by Delan Azabani

++variableincrements the variable, returning the new value.

++variable增加变量,返回新值。

variable++increments the variable, but returns the old value.

variable++增加变量,但返回旧值。

--variabledecrements the variable, returning the new value.

--variable递减变量,返回新值。

variable--decrements the variable, but returns the old value.

variable--递减变量,但返回旧值。

For example:

例如:

a = 5;
b = 5;
c = ++a;
d = b++;

ais 6, bis 6, cis 6 and dis 5.

a是 6,b是 6,c是 6,d是 5。

If you're not using the result, the prefix operators work equally to the postfix operators.

如果您不使用结果,则前缀运算符与后缀运算符的作用相同。

回答by bladnman

I thought for completeness I would add an answer specific to the first of the OP's question:

我认为为了完整性,我会添加一个特定于 OP 问题的第一个答案:

One of your example shows the i++ / ++i being used in a for loop :

您的示例之一显示了在 for 循环中使用的 i++ / ++i :

for (i=1; i<=10; i++) {
  alert(i);
}

you will get 1-10 in your alerts no matter which you use. Example:

无论您使用哪种警报,您都会在警报中获得 1-10。例子:

console.log("i++");
for (i=1; i<=10; i++) {
  console.log(i);
}
console.log("++i");
for (i=1; i<=10; ++i) {
  console.log(i);
}

Paste those into a console window and you can see that they both have the same output.

将它们粘贴到控制台窗口中,您可以看到它们都具有相同的输出。

回答by kaush

i++= Use the value of i in statement then increase it by 1
++i= Increase the value of i by 1 then use in statement.

i++= 在语句中使用 i 的值然后将其
++i增加1 = 将 i 的值增加 1 然后在语句中使用。

回答by chharvey

One case all these answers fail to mention is what happens when i++and ++iare used in operations with other numbers. While the whole “i++is before, ++iis after” concept is easy to grasp when the expression is by itself, it gets much more confusing when you start combining statements. See Examples C and D below.

所有这些答案都没有提到的一种情况是,在与其他数字的运算中使用i++++i使用时会发生什么。虽然当表达式单独使用时i++++i整个“在之前,在之后”的概念很容易理解,但当您开始组合语句时,它会变得更加混乱。参见下面的示例 C 和 D。

// Example A
var i = 42;
var a = i++; // equivalent to `var a = i; i = i+1;`
console.log(a); // 42
console.log(i); // 43

// Example B
var i = 42;
var b = ++i; // equivalent to `i = i+1; var b = i;`
console.log(b); // 43
console.log(i); // 43

// Example C
var i = 42;
var c = i++ * 2; // equivalent to `var c = i*2; i = i+1;`
console.log(c); // 84
console.log(i); // 43

// Example D
var i = 42;
var d = ++i * 2; // equivalent to `i = i+1; var d = i*2;`
console.log(d); // 86
console.log(i); // 43

Notice that in Example C, the i++is not evaluated until aftermultiplication and the assignment of c. This counters the misconception that “i++should be evaluated first in the order of operations.” So in other words the statement i++ * 2actually calculates i * 2beforeit increments i.

请注意,在示例 C 中,i++直到乘法和赋值之后才计算c。这反驳了“i++应该按操作顺序首先评估”的误解。所以换句话说,该语句它递增之前i++ * 2实际计算。i * 2i

回答by jfriend00

It determines whether the increment happens before or after the value of the variable is used.

它确定增量是在使用变量值之前还是之后发生。

var j = 2;
console.log(j++);   // 2
console.log(j);     // 3

var k = 2;
console.log(++k);   // 3
console.log(k);     // 3

回答by Joe

var i = 0;
console.log(i++); // 0
console.log(++i); // 2

回答by LukStorms

I know, this 2011 question has long been answered.

我知道,这个 2011 年的问题早就有了答案。

++variable : Increment variable before using variable
variable++ : Increment variable after using variable

++variable : 使用变量前增加变量
++ : 使用变量后增加变量

But I figured it could still be usefull to include an answer with a snippet to confirm how they behave in a for loop.

但是我认为包含一个带有片段的答案以确认它们在 for 循环中的行为仍然很有用。

Just to verify in your browser that there's really no difference when using a ++i versus a i++ in the for loop declaraction.
And throwing --i versus i-- while we're at it.

只是为了在您的浏览器中验证在 for 循环声明中使用 ++i 与 i++ 时确实没有区别。
并在我们进行时抛出--i vs i--。

console.log("-- with looping --");

console.log("using ++i in a for loop");
for (var i=1; i<=3; ++i) {
  console.log(i);
}

console.log("using i++ in a for loop");
for (var i=1; i<=3; i++) {
  console.log(i);
}

console.log("using --i in a for loop");
for (var i=3; i>=1; --i) {
  console.log(i);
}

console.log("using i-- in a for loop");
for (var i=3; i>=1; i--) {
  console.log(i);
}

console.log("-- without looping --");
var i = 1;
console.log("i: "+ i);
console.log("i++: "+ i++);
console.log("i: "+ i);
console.log("++i: "+ ++i);
console.log("i: "+ i);
console.log("--i: "+ --i);
console.log("i: "+ i);
console.log("i--: "+ i--);
console.log("i: "+ i);