Saturday, June 13, 2026
HomeLanguagesJavascriptJavaScript Program to Check if a String Contains Any Digit Characters

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.

Using for Loop

Iterate through the string character by character using a for loop and check each character’s Unicode value to determine if it’s a digit.

 

Syntax:

  for (let i = 0; i < text.length; i++) {
    if (text[i] >= '0' && text[i] <= '9') {
      return true;
    }
  }

Javascript




function checkDigits(str) {
  for (let i = 0; i < str.length; i++) {
    if (str[i] >= '0' && str[i] <= '9') {
      return true;
    }
  }
  return false;
}
  
const input = "Geeks for Geeks 123 numbers.";
console.log(checkDigits(input));


Output

true

Using Regular Expressions

Use a regular expression to search for any digit characters in the string. Regular expressions provide a concise and powerful way to find patterns in text.

Syntax:

 const digitPattern = /\d/;

Javascript




function checkDigits(str) {
  const digitPattern = /\d/;
  return digitPattern.test(str);
}
  
const input = "Geeks for Geeks";
console.log(checkDigits(input));


Output

false
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS