Number.isInteger()

この記事はまだ日本語に翻訳されていません。MDN の翻訳はボランティアによって行われています。是非 MDN に登録し、私たちの力になって下さい。

This is an experimental technology, part of the Harmony (ECMAScript 6) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

Summary

The Number.isInteger() method determines whether the passed value is an integer.

If the target value is an integer, return true, otherwise return false.
If the value is NaN or infinite, return false.

Syntax

Number.isInteger(testValue)

Parameters

testValue
The value to be tested for being an integer.

Example

Alert of some test values:

alert([
  Number.isInteger(0.1),
  Number.isInteger(1),
  Number.isInteger(Math.PI)
]);

Polyfill

The following algorithm matches the one specified in ECMA harmony:

/*\
|*|
|*|  :: Number.isInteger() polyfill ::
|*|
|*|  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
|*|
\*/

if (!Number.isInteger) {
  Number.isInteger = function isInteger (nVal) {
    return typeof nVal === "number" && isFinite(nVal) && nVal > -9007199254740992 && nVal < 9007199254740992 && Math.floor(nVal) === nVal;
  };
}

Specifications

Specification Status Comment

ECMAScript Language Specification 6th Edition (ECMA-262)

Draft Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 34 [1] 16 (16) Not supported Not supported Not supported
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support Not supported Not supported 16.0 (16) Not supported Not supported Not supported

[1] The feature is available behind a preference. In chrome://flags, activate the entry "Enable Experimental JavaScript".

See also

  • The Number object it belongs to.

Document Tags and Contributors

Contributors to this page: Sheppy, fscholz, ethertank, kscarfone, fusionchess, rvighne
最終更新者: rvighne,