一元加号 (+)
一元加 (+
) 运算符位于其操作数之前并计算其操作数,但尝试计算 将其转换为数字(如果尚未计算)。
¥The unary plus (+
) operator precedes its operand and evaluates to its
operand but attempts to convert it into a number, if it isn't already.
Try it
语法
描述
¥Description
尽管一元否定 (-
) 也可以转换非数字,但一元加是将某些内容转换为数字的最快且首选的方式,因为它不对数字执行任何其他操作。
¥Although unary negation (-
) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number.
一元加法与大多数需要数字的内置方法所使用的正常 数字强制 执行完全相同的步骤。它可以转换整数和浮点数的字符串表示形式,以及非字符串值 true
、false
和 null
。支持十进制和十六进制(0x
前缀)格式的整数。支持负数(但不支持十六进制)。如果它无法解析特定值,它将评估为 NaN
。与同时处理数字和 BigInts 的其他算术运算符不同,对 BigInt 值使用 +
运算符会抛出 TypeError
。
¥Unary plus does the exact same steps as normal number coercion used by most built-in methods expecting numbers. It can convert string representations of integers and floats, as well as the non-string values true
, false
, and null
. Integers in both decimal and hexadecimal (0x
-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN
. Unlike other arithmetic operators, which work with both numbers and BigInts, using the +
operator on BigInt values throws a TypeError
.
示例
与数字一起使用
与非数字一起使用
规范
Specification |
---|
ECMAScript Language Specification # sec-unary-plus-operator |
浏览器兼容性
BCD tables only load in the browser