javascript i ++ vs ++ i [英] javascript i++ vs ++i

查看:95
本文介绍了javascript i ++ vs ++ i的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中我看到 i ++ 在很多情况下使用,我知道它在前面的值中加了一个:

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

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

推荐答案

i ++ <之间的区别/ code>和 ++ i 是表达式的值。

i ++ 是增量前的 i 的值。增量后, ++ i 的值是 i 的值。

The value i++ is the value of i before the increment. The value of ++i is the value of i after the increment.

示例:

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

i - - i 运算符的工作方式相同。

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

这篇关于javascript i ++ vs ++ i的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆