自增运算符 (++
) 将其操作数递增(加1)并返回一个值。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
语法
Operator: x++ or ++x
描述
如使用后置(Postfix)自增,操作符在操作数后(例如 x++
), 操作数将在自增前返回。
如使用前置(Prefix)自增,操作符在操作数前(例如 ++x
), 操作数将先自增后返回。
示例
后置自增(Postfix increment)
let x = 3;
y = x++;
// y = 3
// x = 4
前置自增(Prefix increment)
let a = 2;
b = ++a;
// a = 3
// b = 3
规范
Specification |
---|
ECMAScript (ECMA-262) Increment operator |
浏览器兼容性
BCD tables only load in the browser
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.