此页面由社区从英文翻译而来。了解更多并加入 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描述
减法运算符将两个操作数转换为数值,并根据两个操作数的类型执行数字减法或BigInt 减法。如果类型不匹配,则抛出TypeError。
示例
>数值减法
js
// Number - Number -> subtraction5 - 3; // 2// Number - Number -> subtraction3 - 5; // -2非数值减法
js
// String - Number -> subtraction"foo" - 3; // NaN; "foo" is converted to the number NaN// Number - String -> subtraction5 - "3"; // 2; "3" is converted to the number 3BigInt 减法
js
// BigInt - BigInt -> subtraction2n - 1n; // 1n你不能在减法中混合使用 BigInt 和数字操作数。
js
2n - 1; // TypeError: Cannot mix BigInt and other types, use explicit conversions2 - 1n; // TypeError: Cannot mix BigInt and other types, use explicit conversions规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-subtraction-operator-minus> |