遞減運算子(--)

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),則會先進行遞減,在將遞減後的結果回傳。

範例

遞減運算子置於後綴

js
let x = 3;
y = x--;

// y = 3
// x = 2

遞減運算子置於前綴

js
let a = 2;
b = --a;

// a = 1
// b = 1

規範

Specification
ECMAScript® 2026 Language Specification
# sec-postfix-decrement-operator

瀏覽器相容性

參見