Skip to main content

How to Check if a Number is a Float or an Integer in JavaScript

When working with numbers in JavaScript, it's often necessary to distinguish between an integer (a whole number, like 10) and a floating-point number (a number with a decimal part, like 10.5). This is a common requirement for validation, mathematical calculations, or for handling data from an API.

This guide will teach you how to use the modern, built-in Number.isInteger() method as the primary tool for this task. You will learn how to create simple, reusable functions to reliably check if a value is an integer or a float.

The Core Method: Number.isInteger()

The Number.isInteger() method is the standard and most reliable way to determine if a value is a whole number.

The logic:

  • It returns true if the value is an integer.
  • It returns false for everything else, including floats, strings, NaN, and null.

For example:

console.log(Number.isInteger(42));        // Output: true
console.log(Number.isInteger(-100)); // Output: true
console.log(Number.isInteger(42.0)); // Output: true (because it has no fractional part)

console.log(Number.isInteger(42.5)); // Output: false
console.log(Number.isInteger('42')); // Output: false (it's a string)
console.log(Number.isInteger(Infinity)); // Output: false
note

The key takeaway is that Number.isInteger() is a very strict and predictable check.

Solution 1: Check if a Value is an Integer

Using the Number.isInteger() method directly is the best way to check for an integer. For robustness, you can wrap it in a function.

function isInteger(value) {
return Number.isInteger(value);
}

console.log(isInteger(123)); // Output: true
console.log(isInteger(12.0)); // Output: true
console.log(isInteger(12.3)); // Output: false
console.log(isInteger('123')); // Output: false
note

This is the simplest and most correct way to check if a value is an integer.

Solution 2: Check if a Value is a Float

There is no built-in Number.isFloat() method in JavaScript. To check for a float, we must use a combination of checks.

The logic: A value is a float if it meets all of the following three conditions:

  1. Its typeof must be 'number'.
  2. It must not be NaN (Not-a-Number).
  3. It must not be an integer (Number.isInteger() must be false).

Solution:

function isFloat(value) {
if (
typeof value === 'number' &&
!Number.isNaN(value) &&
!Number.isInteger(value)
) {
return true;
}

return false;
}

// ✅ Correct results for floats
console.log(isFloat(12.34)); // Output: true
console.log(isFloat(-0.5)); // Output: true

// ⛔️ Correct results for non-floats
console.log(isFloat(12.0)); // Output: false (is an integer)
console.log(isFloat(12)); // Output: false (is an integer)
console.log(isFloat('12.3')); // Output: false (is a string)
console.log(isFloat(NaN)); // Output: false

How the Methods Work and Their Key Differences

  • Number.isInteger(): This is a static method on the Number object that performs a strict check. It does not perform any type coercion, which is why Number.isInteger('42') is false. It's a reliable and precise tool.
  • isFloat() Function: Our custom function works by a process of elimination. The typeof value === 'number' is a broad check that allows numbers, NaN, and Infinity. We then use !Number.isNaN() and !Number.isInteger() to narrow it down, leaving only the true floating-point numbers.

A Note on Performance and Readability

For this common task, the built-in Number.isInteger() and the custom isFloat() function are extremely performant. They are also the most readable and universally understood solutions in modern JavaScript. While you could use a regular expression to check if a string representation of a number contains a dot, that approach is less direct and doesn't work on number types without first converting them to strings. The methods presented here are the best practice.

Conclusion

Distinguishing between integers and floats is a straightforward task in JavaScript with the right built-in tools.

The key takeaways are:

  1. To check if a value is an integer, use the built-in Number.isInteger(value) method. It is the most reliable and direct approach.
  2. To check if a value is a float, you must use a combination of checks: the value must be a number, must not be NaN, and must not be an integer.
  3. Be aware that Number.isInteger(10.0) is true, as the value has no fractional part.