此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
自增(++)
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
自增(++)运算符对其操作数进行自增(加一),并根据运算符的位置返回自增之前或之后的值。
In this article
尝试一下
let x = 3;const y = x++;console.log(`x:${x}, y:${y}`);// Expected output: "x:4, y:3"let a = 3;const b = ++a;console.log(`a:${a}, b:${b}`);// Expected output: "a:4, b:4"语法
js
x++++x描述
如果使用后缀式,即将运算符放在操作数的后面(例如x++),操作数会加一,然后返回加一之前的值。
如果使用前缀式,即将运算符放在操作数的前面(例如++x),操作数会加一,然后返回加一之后的值。
自增运算符只能应用于引用的操作数(变量和对象属性,即有效的赋值目标)。++x 本身的计算结果是一个值,而不是一个引用,因此不能将多个自增运算符链接在一起。
js
++(++x); // SyntaxError: Invalid left-hand side expression in prefix operation示例
>后缀式
js
let x = 3;const y = x++;// x = 4// y = 3前缀式
js
let x = 3;const y = ++x;// x = 4// y = 4规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-postfix-increment-operator> |