Example 1: Using Number.isInteger()
Output
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
In the above program, the passed value is checked if it is an integer value or a float value.
- The
typeofoperator 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.
Example 2: Using Regex
Output
44 is an integer. -44 is an integer. 3.4 is a float value. -3.4 is a float value.
In the above example, the regex pattern is used to check if the passed argument is an integer value or float value.
The pattern /^-?[0-9]+$/ looks for the integer value.
The test() method of the RegExp object is used to test the pattern with the given value.
Note: The above program only works for numbers.