isFinite()
isFinite()
函数确定某个值是否是有限的,如有必要,首先将该值转换为数字。有限数是指不是 NaN
或 ±Infinity
的数。因为 isFinite()
函数内部的强制转换可以是 surprising,所以你可能更喜欢使用 Number.isFinite()
。
¥The isFinite()
function determines whether a value is finite, first converting the value to a number if necessary. A finite number is one that's not NaN
or ±Infinity
. Because coercion inside the isFinite()
function can be surprising, you may prefer to use Number.isFinite()
.
Try it
语法
参数
返回值
描述
¥Description
isFinite()
是全局对象的函数属性。
¥isFinite()
is a function property of the global object.
当 isFinite()
函数的参数不是 数字 类型时,该值首先被强制转换为数字,然后将结果值与 NaN
和 ±Infinity 进行比较。这与 isNaN
的行为一样令人困惑 - 例如,isFinite("1")
是 true
。
¥When the argument to the isFinite()
function is not of type Number, the value is first coerced to a number, and the resulting value is then compared against NaN
and ±Infinity. This is as confusing as the behavior of isNaN
— for example, isFinite("1")
is true
.
Number.isFinite()
是测试某个值是否为有限数值的更可靠方法,因为它对于任何非数字输入都会返回 false
。
¥Number.isFinite()
is a more reliable way to test whether a value is a finite number value, because it returns false
for any non-number input.
示例
使用 isFinite()
规范
Specification |
---|
ECMAScript Language Specification # sec-isfinite-number |
浏览器兼容性
BCD tables only load in the browser