Categories
JavaScript Answers

How to check if character is number with JavaScript?

Spread the love

Sometimes, we want to check if character is number with JavaScript.

In this article, we’ll look at how to check if character is number with JavaScript.

How to check if character is number with JavaScript?

To check if character is number with JavaScript, we can use the regex test method.

For instance, we write

const isNumeric = (str) => {
  return /^\d+$/.test(str);
};

to define the isNumeric function that checks if str is a number with /^\d+$/.test.

\d+ matches strings that has all digits inside.

Conclusion

To check if character is number with JavaScript, we can use the regex test method.

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 *