自减(--)

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

We’d love to hear your thoughts on the next set of proposals for the JavaScript language. You can find a description of the proposals here.
Please take two minutes to fill out our short survey.

自减--)运算符对其操作数进行自减(减一),并根据运算符的位置返回自减之前或之后的值。

尝试一下

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

浏览器兼容性

参见