相減運算子(-)

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.

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

嘗試一下

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® 2025 Language Specification
# sec-subtraction-operator-minus

瀏覽器相容性

BCD tables only load in the browser

參見