一元負號運算子(-)
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 |
瀏覽器相容性
BCD tables only load in the browser