← 返回首页
相減運算子(-) - JavaScript | MDN

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

View in English Always 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月.

相減運算子(-)是用來將兩個值進行相減,並得出它們的差值。

In this article

嘗試一下

console.log(5 - 3); // Expected output: 2 console.log(3.5 - 5); // Expected output: -1.5 console.log(5 - "hello"); // Expected output: NaN console.log(5 - true); // Expected output: 4

語法

js
x - y

範例

數字相減

js
5 - 3; // 2 3 - 5; // -2

非數字之間的相減

js
"foo" - 3; // NaN

規範

Specification
ECMAScript® 2027 Language Specification
# sec-subtraction-operator-minus

瀏覽器相容性

Enable JavaScript to view this browser compatibility table.

參見