How to use Regular Expressions In Javascript

This approach involves converting the number to a string and using a Regular Expression to check if it contains a decimal point. If it does, it’s a float; otherwise, it’s an integer.

Syntax

/\d+\.\d+/.test(numberString);

Javascript




const inputNumber = 123.456;
const numberString = inputNumber.toString();
const isFloat = /\d+\.\d+/.test(
    numberString
);
console.log(
    `Is ${inputNumber} a float? ${isFloat}`
);


Output

Is 123.456 a float? true


JavaScript Program to Check if a Number is Float or Integer

In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point.

Table of Content

  • Using the Number.isInteger() Method
  • Using the Modulus Operator (%)
  • Using Regular Expressions

We will explore every approach to Check if a Number is a Float or Integer, along with understanding their basic implementations.

Similar Reads

Using the Number.isInteger() Method

...

Using the Modulus Operator (%)

The Number.isInteger() Method checks if the given number is an integer or not. It returns true if the number is an integer, and false if it’s a float or not a number at all....

Using Regular Expressions

...