This tutorial will see a JavaScript program to check if the given number is a Float or Integer.
JavaScript being loosely typed stores all the data types in a single format variable. The variable can be re-assigned to any other type and JavaScript won’t complain.
Thus in many cases, we have to explicitly check the type of variables. For example, if the current variable holds a number, we need to determine if the number is an Integer or Float value.
For this, we will create a function and check if the variable is a number, and if it is a number then its type.
JavaScript numbers have an inbuilt method isInteger that checks if the value is an integer and returns a boolean value.
const isFloat = (num) => { // check if the input value is a number if(typeof num == 'number' && !isNaN(num)){ // check if it is float // alter this condition to check the integer if (!Number.isInteger(num)) { return true } } return false; } console.log(isFloat(100)); // false console.log(isFloat(100.1)); // true console.log(isFloat(null)); // false