此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
相減運算子(-)
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月.
相減運算子(-)是用來將兩個值進行相減,並得出它們的差值。
In this article
嘗試一下
console.log(5 - 3);// Expected output: 2console.log(3.5 - 5);// Expected output: -1.5console.log(5 - "hello");// Expected output: NaNconsole.log(5 - true);// Expected output: 4語法
js
x - y範例
>數字相減
js
5 - 3; // 23 - 5; // -2非數字之間的相減
js
"foo" - 3; // NaN規範
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-subtraction-operator-minus> |