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.