Number.isFinite()

Number.isFinite() 静态方法确定传递的值是否是有限数 - 也就是说,它检查给定值是否是一个数字,并且该数字既不是正 Infinity,负 Infinity,也不是 NaN

¥The Number.isFinite() static method determines whether the passed value is a finite number — that is, it checks that a given value is a number, and the number is neither positive Infinity, negative Infinity, nor NaN.

Try it

语法

¥Syntax

js
Number.isFinite(value)

参数

¥Parameters

value

要测试有限性的值。

返回值

¥Return value

如果给定值是有限数,则为布尔值 true。否则 false

¥The boolean value true if the given value is a finite number. Otherwise false.

示例

¥Examples

使用 isFinite()

¥Using isFinite()

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

Number.isFinite(0); // true
Number.isFinite(2e64); // true

Number.isFinite() 和全局 isFinite() 之间的区别

¥Difference between Number.isFinite() and global isFinite()

与全局 isFinite() 函数相比,此方法不会首先将参数转换为数字。这意味着只有 number 和 类型的值是有限返回 true,非数字总是返回 false

¥In comparison to the global isFinite() function, this method doesn't first convert the parameter to a number. This means only values of the type number and are finite return true, and non-numbers always return false.

js
isFinite("0"); // true; coerced to number 0
Number.isFinite("0"); // false
isFinite(null); // true; coerced to number 0
Number.isFinite(null); // false

规范

Specification
ECMAScript Language Specification
# sec-number.isfinite

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看