++ someVariable Vs. Javascript中的someVariable ++ [英] ++someVariable Vs. someVariable++ in Javascript

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

问题描述

在Javascript中,您可以在变量名之前或之后使用 ++ 运算符。这些增量变量的方法之间有什么区别?

In Javascript you can use ++ operator before or after the variable name. What, if any, are the differences between these ways of incrementing a variable?

推荐答案

与其他语言相同:


  • ++ x (预增量)表示增加变量;值的变量表达式是最终值

  • x ++ (后递增)表示记住原始值,然后递增变量;值表达式是原始值

  • ++x (pre-increment) means "increment the variable; the value of the expression is the final value"
  • x++ (post-increment) means "remember the original value, then increment the variable; the value of the expression is the original value"

现在用作独立语句时,它们的意思相同:

Now when used as a standalone statement, they mean the same thing:

x++;
++x;

当你在其他地方使用表达式的值时会出现差异。例如:

The difference comes when you use the value of the expression elsewhere. For example:

x = 0;
y = array[x++]; // This will get array[0]

x = 0;
y = array[++x]; // This will get array[1]

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

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