一元負號運算子(-)
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
一元負號運算子(-)置於運算元之前,並將運算元轉為其負值。
嘗試一下
const x = 4; const y = -x; console.log(y); // Expected output: -4 const a = "4"; const b = -a; console.log(b); // Expected output: -4 語法
js
-x 範例
>轉換數字
js
const x = 3; const y = -x; // y = -3 // x = 3 轉換非數字
一元負號運算子可以將非數字轉為數字。
js
const x = "4"; const y = -x; // y = -4 規範
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-unary-minus-operator> |