Movatterモバイル変換


[0]ホーム

URL:


  1. 給開發者的 Web 技術文件
  2. JavaScript
  3. JavaScript 參考文件
  4. 運算式與運算子
  5. 一元負號運算子(-)

此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in EnglishAlways switch to English

一元負號運算子(-)

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: -4const 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

瀏覽器相容性

參見

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp