此页面由社区从英文翻译而来。了解更多并加入 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:2, y:3"let a = 3;const b = --a;console.log(`a:${a}, b:${b}`);// Expected output: "a:2, b:2"语法
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 = 2// y = 3前缀式
js
let x = 3;const y = --x;// x = 2// y = 2规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-postfix-decrement-operator> |