JavaScript Number.isInteger()

Number.isInteger(value) returns true when value is a JavaScript number with no fractional part. It does not convert strings to numbers.

console.log(Number.isInteger(5));     // true
console.log(Number.isInteger(5.5));   // false
console.log(Number.isInteger("5"));   // false

Number.isInteger() syntax Top ↑

Number.isInteger(value)

The method returns a Boolean and performs no type coercion.

Positive, negative and decimal examples Top ↑

console.log(Number.isInteger(5));      // true
console.log(Number.isInteger(-5));     // true
console.log(Number.isInteger(0));      // true
console.log(Number.isInteger(5.5));    // false
console.log(Number.isInteger(-5.5));   // false
console.log(Number.isInteger(NaN));    // false
console.log(Number.isInteger(Infinity)); // false

Why Number.isInteger(5.0) is true Top ↑

JavaScript has one ordinary Number type for both integers and floating-point values. 5 and 5.0 represent the same numeric value.

console.log(Number.isInteger(5.0)); // true

Numeric strings are not integers until converted Top ↑

const raw = "42";
console.log(Number.isInteger(raw));         // false
const value = Number(raw);
console.log(Number.isInteger(value));       // true

Number.isInteger() vs Number.isSafeInteger() Top ↑

Number.isInteger() checks whether a Number has an integer value. Number.isSafeInteger() additionally checks whether that integer can be represented exactly within JavaScript's safe-integer range.

const n = Number.MAX_SAFE_INTEGER + 1;
console.log(Number.isInteger(n));     // true
console.log(Number.isSafeInteger(n)); // false

Floating-point precision can affect the result Top ↑

Because JavaScript Numbers use binary floating-point representation, a value that looks fractional in source code can sometimes round to an integer at very large magnitudes. Treat Number.isInteger() as a check of the stored Number value, not a guarantee about the original text entered by a user.

Validate an integer entered in a form Top ↑

function readInteger(raw) {
  if (raw.trim() === "") return null;
  const value = Number(raw);
  return Number.isInteger(value) ? value : null;
}

See NaN, numeric validation, and factors exercise.

JavaScript Math Reference




Subscribe to our YouTube Channel here



plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer