JavaScript Program to Check if a Number is Float or Integer

JavaScript Program to Check if a Number is Float or Integer The article is a lively article because it contains interesting information and your favorite.   

JavaScript Program to Check if a Number is Float or Integer

JavaScript Program to Check if a Number is Float or Integer

// program to check if a number is a float or integer value

function checkNumber(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`);
    }
}

checkNumber('hello');
checkNumber(44);
checkNumber(3.4);
checkNumber(-3.4);
checkNumber(NaN);
hello is not a number
44 is integer.
3.4 is a float value.
-3.4 is a float value.
NaN is not a number
// program to check if a number is a float or integer value

function checkNumber(x) {

    let regexPattern = /^-?[0-9]+$/;
    
    // check if the passed number is integer or float
    let result = regexPattern.test(x);
    
    if(result) {
        console.log(`${x} is an integer.`);
    }
    else {
        console.log(`${x} is a float value.`)
    }

}

checkNumber(44);
checkNumber(-44);
checkNumber(3.4);
checkNumber(-3.4);
44 is an integer.
-44 is an integer.
3.4 is a float value.
-3.4 is a float value.

Read Also: Find ASCII Value of Character in JavaScript

Final Words

I hope you find the article JavaScript Program to Check if a Number is Float or Integer uses. The reason is that we have told you all the information through this article in a way that you can understand. And if you have any doubts, you can express your doubts through the comment box. We also ask that you help share this article with your friends.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment