Signup/Sign In
PUBLISHED ON: FEBRUARY 21, 2023

JavaScript Program to Check if a Number is Float or Integer

This tutorial will teach how to make a JavaScript Program to Check if a Number is Float or Integer. To understand this article we need to have a decent knowledge of JavaScript Regex, Number.isInteger(), and typeof Operator. It's an easy job that can be done with a few lines of code. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

Using Number.isInteger()

In the code given below, the passed value is checked if it is an Integer Value or a Float Value.

  • The typeof operator is used to check the data type of the passed value.
  • The isNaN() method checks if the passed value is a number.
  • The Number.isInteger() method is used to check if the number is an integer value.
// JavaScript 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(4488);
checkNumber(3.1417);
checkNumber(-3.4);
checkNumber(NaN);
checkNumber('JavaScript Program');


4488 is integer.
3.1417 is a float value.
-3.4 is a float value.
NaN is not a number
JavaScript Program is not a number



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.