遞增運算子(++)
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.
遞增運算子(++
)遞增(加一)它的運算元並將結果回傳。
嘗試一下
let x = 3;
const y = x++;
console.log(`x:${x}, y:${y}`);
// Expected output: "x:4, y:3"
let a = 3;
const b = ++a;
console.log(`a:${a}, b:${b}`);
// Expected output: "a:4, b:4"
語法
js
x++
++x
詳細說明
若將遞增運算子作為後綴(例如 x++
),則會先回傳原本的值,再進行遞增。
若作為前綴(例如 ++x
),則會先進行遞增,再將遞增後的結果回傳。
範例
遞增運算子置於後綴
js
let x = 3;
y = x++;
// y = 3
// x = 4
遞增運算子置於前綴
js
let a = 2;
b = ++a;
// a = 3
// b = 3
規範
Specification |
---|
ECMAScript® 2025 Language Specification # sec-postfix-increment-operator |
瀏覽器相容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Increment ( ++ ) |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.