一元負號運算子(-)

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 GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
Unary negation (-)

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

參見