JavaScript Answers

How to check if a string is a float with JavaScript?

Spread the love

To check if a string is a float with JavaScript, we can use the isNaN function.

For instance, we write

if (!isNaN(value) && value.toString().includes(".")) {
  console.log("this is a float.");

to check if value is a number by negating the boolean returned by isNaN.

And then we check if a decimal point is included by converting value to a string with toString and call the includes method on the string.

If both are true, then value is a float.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *