一元負號運算子(-)
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
範例
轉換數字
js
const x = 3;
const y = -x;
// y = -3
// x = 3
轉換非數字
一元負號運算子可以將非數字轉為數字。
js
const x = "4";
const y = -x;
// y = -4
規範
Specification |
---|
ECMAScript Language Specification # sec-unary-minus-operator |
瀏覽器相容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Unary negation ( - ) |
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.