遞增運算子(++)
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 = 4
遞增運算子置於前綴
js
let a = 2;
b = ++a;
// a = 3
// b = 3
規範
Specification |
---|
ECMAScript Language Specification # sec-postfix-increment-operator |
瀏覽器相容性
BCD tables only load in the browser