Number.isInteger()

Number.isInteger() 方法用来判断给定的参数是否为整数。

语法

Number.isInteger(value)

参数

value
要判断此参数是否为整数

返回值

判断给定值是否是整数的 Boolean 值。

描述

如果被检测的值是整数,则返回 true,否则返回 false。注意 NaN 和正负 Infinity 不是整数。

示例

Number.isInteger(0);         // true
Number.isInteger(1);         // true
Number.isInteger(-100000);   // true

Number.isInteger(0.1);       // false
Number.isInteger(Math.PI);   // false

Number.isInteger(Infinity);  // false
Number.isInteger(-Infinity); // false
Number.isInteger("10");      // false
Number.isInteger(true);      // false
Number.isInteger(false);     // false
Number.isInteger([1]);       // false

Polyfill

Number.isInteger = Number.isInteger || function(value) {
    return typeof value === "number" && 
           isFinite(value) && 
           Math.floor(value) === value;
};

规范

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

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
isIntegerChrome Full support 34Edge Full support 12Firefox Full support 16IE No support NoOpera Full support 21Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 16Opera Android Full support 21Safari iOS Full support 9Samsung Internet Android Full support 2.0nodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support

相关链接