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

语法

¥Syntax

js
isFinite(value)

参数

¥Parameters

value

要测试的值。

返回值

¥Return value

如果给定值在 转换为数字 后是 NaNInfinity-Infinity,则为 false;否则,true

¥false if the given value is NaN, Infinity, or -Infinity after being converted to a number; otherwise, true.

描述

¥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.

示例

¥Examples

使用 isFinite()

¥Using isFinite()

js
isFinite(Infinity); // false
isFinite(NaN); // false
isFinite(-Infinity); // false

isFinite(0); // true
isFinite(2e64); // true
isFinite(910); // true

// Would've been false with the more robust Number.isFinite():
isFinite(null); // true
isFinite("0"); // true

规范

Specification
ECMAScript Language Specification
# sec-isfinite-number

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看