Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To check number is an integer or float, JavaScript provides isInteger() method just pass your number as a parameter and it will return true if the number is an integer or else false.

Today, I’m going to show How do I check if the number is an integer or float in Javascript, here I will use the javascript string isInteger() method, to check if a number is an integer or float.

What is a isInteger() method?

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

Let’s start the today’s tutorial How do you check if the number is integer or float in JavaScript?

In the following example, we will create our custom function to check number is int or float, where we will also add some more conditions like the given parameter should be a number and after that, we will check number is integer or float.

// function to check if a number is a float or integer value
function validateNumber(x) {
    // check if the passed value is a number
    if(typeof x == 'number' && !isNaN(x)){
        // check if it is integer
        if (Number.isInteger(x)) {
            console.log(`${x} is integer.`);
        }
        else {
            console.log(`${x} is a float value.`);
        }
    } else {
        console.log(`${x} is not a number`);
    }
}

validateNumber('hello');
validateNumber(44);
validateNumber(3.4);
validateNumber(-3.4);
validateNumber(NaN);

In the above program, we have taken examples of every possible value like parameter if string, number, positive value, a negative value, or NaN values. let’s check the output.

javascript, check if number is integer or float example
javascript, check if number is integer or float example

I hope it’s help you, All the best 👍.