How to use filter() and isNaN() In Javascript
This approach Uses the filter() method to create an array of digit characters and check if the array’s length is greater than 0.
Syntax:
str.split('').filter(char => !isNaN(parseInt(char))).length > 0
Example: In this example the function splits the string into characters, filters non-digit characters, and returns true if any digit is found.
function containsDigit(str) {
return (
str
.split("")
.filter(
(char) => !isNaN(parseInt(char))
).length > 0
);
}
let str = "Hello w3wiki123";
console.log(containsDigit(str));
Output
true
JavaScript Program to Check if a String Contains Any Digit Characters
In this article, we will see how to check if a string contains any digit characters in JavaScript. Checking if a string contains any digit characters (0-9) in JavaScript is a common task when we need to validate user input or perform specific actions based on the presence of digits in a string.
We will explore every approach to check if a string contains any digit characters, along with understanding their basic implementations.
Table of Content
- Using for Loop
- Using Regular Expressions
- Using filter() and isNaN()
- Using String.prototype.match
- Using Array.prototype.some
- Using the every Method
- Using Array.prototype.find
Examples of Checking if a String Contains Any Digit Characters
Contact Us