遞減運算子(--)

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.

遞減運算子(--)遞減(減一)它的運算元並將結果回傳。

嘗試一下

語法

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 Language Specification
# sec-postfix-decrement-operator

瀏覽器相容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
Decrement (--)

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

參見