How to validate digits, including floats, in JavaScript?

Sometimes, we want to validate digits, including floats, in JavaScript.

In this article, we’ll look at how to validate digits, including floats, in JavaScript.

How to validate digits, including floats, in JavaScript?

To validate digits, including floats, in JavaScript, we can use the isNaN function to check if a value can be converted to a number.

For instance, we write:

isNaN('0');

which returns false, which means '0' can be converted to a number.

Or:

isNaN('foo')

which returns true, which means 'foo' can’t be converted to a number.

Conclusion

To validate digits, including floats, in JavaScript, we can use the isNaN function to check if a value can be converted to a number.