Number.isFinite()

Number.isFinite() 方法用来检测传入的参数是否是一个有穷数。

语法

Number.isFinite(value)

参数

value
要被检测有穷性的值。

返回值

一个 布尔值 表示给定的值是否是一个有穷数。

描述

和全局的 isFinite() 函数相比,这个方法不会强制将一个非数值的参数转换成数值,这就意味着,只有数值类型的值,且是有穷的(finite),才返回 true

Polyfill

if (Number.isFinite === undefined) Number.isFinite = function(value) {
    return typeof value === 'number' && isFinite(value);
}

示例

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

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

Number.isFinite('0');       // false, would've been true with
                            // global isFinite('0')
Number.isFinite(null);      // false, would've been true with
                            // global isFinite(null)

规范

规范名称 规范状态 备注
ECMAScript 2015 (6th Edition, ECMA-262)
Number.isInteger
Standard
ECMAScript (ECMA-262)
Number.isInteger
Living Standard

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
isFiniteChrome Full support 19Edge Full support 12Firefox Full support 16IE No support NoOpera Full support 15Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 25Firefox Android Full support 16Opera Android Full support 14Safari iOS Full support 9Samsung Internet Android Full support 1.5nodejs Full support 0.10

Legend

Full support  
Full support
No support  
No support

相关链接