How do I check that a number is float or integer - JavaScript?

In JavaScript, you can check if a number is a float (decimal) or integer using various methods. The most reliable approach uses the modulo operator to detect decimal values.

Method 1: Using Modulo Operator

The modulo operator % returns the remainder after division. For floats, value % 1 returns the decimal part:

function checkNumberIfFloat(value) {
    return Number(value) === value && value % 1 !== 0;
}

var value1 = 10;
var value2 = 10.15;

if (checkNumberIfFloat(value1)) {
    console.log("The value is float=" + value1);
} else {
    console.log("The value is not float=" + value1);
}

if (checkNumberIfFloat(value2)) {
    console.log("The value is float=" + value2);
} else {
    console.log("The value is not float=" + value2);
}
The value is not float=10
The value is float=10.15

Method 2: Using Number.isInteger()

The Number.isInteger() method directly checks if a value is an integer:

function isFloat(value) {
    return Number.isFinite(value) && !Number.isInteger(value);
}

console.log(isFloat(10));        // false - integer
console.log(isFloat(10.15));     // true - float
console.log(isFloat(0));         // false - integer
console.log(isFloat(3.0));       // false - treated as integer
console.log(isFloat("10.5"));    // false - string, not number
false
true
false
false
false

Method 3: Using Math.floor() Comparison

Compare the number with its floor value to detect decimals:

function isFloat(value) {
    return typeof value === 'number' && value !== Math.floor(value);
}

console.log(isFloat(42));         // false
console.log(isFloat(42.7));       // true  
console.log(isFloat(-3.14));      // true
console.log(isFloat(0.0));        // false
false
true
true
false

Comparison

Method Handles Edge Cases Performance Readability
Modulo operator Good Fast Medium
Number.isInteger() Excellent Fast High
Math.floor() Good Medium High

Conclusion

Use Number.isInteger() combined with Number.isFinite() for the most reliable float detection. The modulo method works well for simple cases, while Math.floor() offers good readability.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements