Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Number
  6. isInteger()

Number.isInteger()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨September 2015⁩.

TheNumber.isInteger() static method determines whether the passed value is an integer.

Try it

function fits(x, y) {  if (Number.isInteger(y / x)) {    return "Fits!";  }  return "Does NOT fit!";}console.log(fits(5, 10));// Expected output: "Fits!"console.log(fits(5, 11));// Expected output: "Does NOT fit!"

Syntax

js
Number.isInteger(value)

Parameters

value

The value to be tested for being an integer.

Return value

The boolean valuetrue if the given value is an integer. Otherwisefalse.

Description

If the target value is an integer, returntrue, otherwise returnfalse. If the value isNaN orInfinity, returnfalse. The method will also returntrue for floating point numbers that can be represented as integer. It will always returnfalse if the value is not a number.

Note that some number literals, while looking like non-integers, actually represent integers — due to the precision limit of ECMAScript floating-point number encoding (IEEE-754). For example,5.0000000000000001 only differs from5 by1e-16, which is too small to be represented. (For reference,Number.EPSILON stores the distance between 1 and the next representable floating-point number greater than 1, and that is about2.22e-16.) Therefore,5.0000000000000001 will be represented with the same encoding as5, thus makingNumber.isInteger(5.0000000000000001) returntrue.

In a similar sense, numbers around the magnitude ofNumber.MAX_SAFE_INTEGER will suffer from loss of precision and makeNumber.isInteger returntrue even when it's not an integer. (The actual threshold varies based on how many bits are needed to represent the decimal — for example,Number.isInteger(4500000000000000.1) istrue, butNumber.isInteger(4500000000000000.5) isfalse.)

Examples

Using isInteger

js
Number.isInteger(0); // trueNumber.isInteger(1); // trueNumber.isInteger(-100000); // trueNumber.isInteger(99999999999999999999999); // trueNumber.isInteger(0.1); // falseNumber.isInteger(Math.PI); // falseNumber.isInteger(NaN); // falseNumber.isInteger(Infinity); // falseNumber.isInteger(-Infinity); // falseNumber.isInteger("10"); // falseNumber.isInteger(true); // falseNumber.isInteger(false); // falseNumber.isInteger([1]); // falseNumber.isInteger(5.0); // trueNumber.isInteger(5.000000000000001); // falseNumber.isInteger(5.0000000000000001); // true, because of loss of precisionNumber.isInteger(4500000000000000.1); // true, because of loss of precision

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-number.isinteger

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp