此頁面由社群從英文翻譯而來。了解更多並加入 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),則會先進行遞減,在將遞減後的結果回傳。
範例
>遞減運算子置於後綴
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> |