在一个基本的javascript for循环中,++i和i++ +的区别是什么

时间:2021-12-06 12:05:05

In a basic for loop, what is the difference between

在一个基本的for循环中,有什么区别

var arr = [0,1,2,3,4];
for(var i = 0, len = arr.length; i < len; ++i) {
   console.log(i);
}

and

var arr = [0,1,2,3,4];
for(var i = 0, len = arr.length; i < len; i++) {
   console.log(i);
}

(The difference is just in the ++i and i++)

(不同之处在于++i和i++)

I see both used everywhere. It seems to me they both produce the exact same result. If this is the case, is there a preference for either one?

我看到两者都在使用。在我看来,它们都产生了完全相同的结果。如果是这种情况,有没有偏好哪一个?

3 个解决方案

#1


3  

There's no difference. The only difference between pre-increment and post-increment is if you're assigning the result to something; pre-increment assigns the new value, post-increment assigns the old value.

没有区别。前增量和后增量之间的唯一区别是如果你把结果赋给某个东西;预增量分配新值,后增量分配旧值。

#2


1  

pre-increment (++i) adds one to the value of i, then returns i; in contrast, i++ returns i then adds one to it, which in theory results in the creation of a temporary variable storing the value of i before the increment operation was applied.

前增量(++i)将1添加到i的值中,然后返回i;相反,i++返回i,然后添加一个到它,这在理论上导致在应用增量操作之前创建一个临时变量来存储i的值。

There change i++ to ++i to optimize.

这里将i++ ++i变为+i进行优化。

#3


0  

According to ECMA Script 5.1 Standard Specification for for loop,

根据ECMA Script 5.1 for loop标准规范,

f. If the second Expression is present, then.

f.如果存在第二个表达式,则。

i. Let incExprRef be the result of evaluating the second Expression.

i.让incExprRef是求第二个表达式的结果。

ii. Call GetValue(incExprRef). (This value is not used.)

二世。调用GetValue(incExprRef)。(不使用此值)

The expressions in alteration part are evaluated, but their values are ignored. So, ++i and i++ will not make any difference here.

修改部分的表达式被求值,但是它们的值被忽略。所以,+i和i++在这里没有任何区别。

Also, ++i and i++ are evaluated almost the same. So, I hardly think there will be any difference in performance front as well.

同时,++i和++的值几乎相同。所以,我不认为前面的表现会有什么不同。

#1


3  

There's no difference. The only difference between pre-increment and post-increment is if you're assigning the result to something; pre-increment assigns the new value, post-increment assigns the old value.

没有区别。前增量和后增量之间的唯一区别是如果你把结果赋给某个东西;预增量分配新值,后增量分配旧值。

#2


1  

pre-increment (++i) adds one to the value of i, then returns i; in contrast, i++ returns i then adds one to it, which in theory results in the creation of a temporary variable storing the value of i before the increment operation was applied.

前增量(++i)将1添加到i的值中,然后返回i;相反,i++返回i,然后添加一个到它,这在理论上导致在应用增量操作之前创建一个临时变量来存储i的值。

There change i++ to ++i to optimize.

这里将i++ ++i变为+i进行优化。

#3


0  

According to ECMA Script 5.1 Standard Specification for for loop,

根据ECMA Script 5.1 for loop标准规范,

f. If the second Expression is present, then.

f.如果存在第二个表达式,则。

i. Let incExprRef be the result of evaluating the second Expression.

i.让incExprRef是求第二个表达式的结果。

ii. Call GetValue(incExprRef). (This value is not used.)

二世。调用GetValue(incExprRef)。(不使用此值)

The expressions in alteration part are evaluated, but their values are ignored. So, ++i and i++ will not make any difference here.

修改部分的表达式被求值,但是它们的值被忽略。所以,+i和i++在这里没有任何区别。

Also, ++i and i++ are evaluated almost the same. So, I hardly think there will be any difference in performance front as well.

同时,++i和++的值几乎相同。所以,我不认为前面的表现会有什么不同。