Check if a String Contains Only Digits

medium

By - Aman Pareek

Last Updated - 28/08/2024

Problem Statement

Write a function that checks whether a given string consists exclusively of digit characters (i.e., 0 through 9). The function should return true if the string contains only digits, and false

Example 1

Input: str = "689454545454"

Output: true

Example 2

Input: str = "he is 24 years old"

Output: false

Solution 1: Using Regular Expression

function isOnlyDigitsUsingRegex(str) {
  if (typeof str !== 'string') {
    throw new Error("Your input is not a string");
  }
  const re = /^\d+$/;
  return re.test(str);
} 

const str1 = "689454545454";
isOnlyDigitsUsingRegex(str1);  //output: true 

const str2 = "he is 24 years old";
isOnlyDigitsUsingRegex(str2);  //output: false 

Solution 2: Using Array.prototype.every

function isOnlyDigitsUsingEvery(str) {
  if (typeof str !== 'string') {
    throw new Error("Your input is not a string");
  }
  return [...str].every(char => char >= '0' && char <= '9');
} 

const str1 = "689454545454";
isOnlyDigitsUsingEvery(str1);  //output: true 

const str2 = "he is 24 years old";
isOnlyDigitsUsingEvery(str2);  //output: false 

Solution 3: Using Array.prototype.some

function isOnlyDigitsUsingSome(str) {
  if (typeof str !== 'string') {
    throw new Error("Your input is not a string");
  }
  return ![...str].some(char => char < '0' || char > '9');
} 

const str1 = "689454545454";
isOnlyDigitsUsingSome(str1);  //output: true 

const str2 = "he is 24 years old";
isOnlyDigitsUsingSome(str2);  //output: false 

Solution 4: Using Array.prototype.filter

function isOnlyDigitsUsingFilter(str) {
  if (typeof str !== 'string') {
    throw new Error("Your input is not a string");
  }
  return str.split("").filter(char => char < '0' || char > '9').length === 0;
} 

const str1 = "689454545454";
isOnlyDigitsUsingFilter(str1);  //output: true 

const str2 = "he is 24 years old";
isOnlyDigitsUsingFilter(str2);  //output: false 

Solution 5: Using for Loop

function isOnlyDigitsUsingForLoop(str) {
  if (typeof str !== 'string') {
    throw new Error("Your input is not a string");
  }
  for (let i = 0; i < str.length; i++) {
    if (str[i] < '0' || str[i] > '9') {
      return false;
    }
  }
  return true;
} 

const str1 = "689454545454";
isOnlyDigitsUsingForLoop(str1);  //output: true 

const str2 = "he is 24 years old";
isOnlyDigitsUsingForLoop(str2);  //output: false 

Solution 6: Using String.prototype.match

function isOnlyDigitsUsingMatch(str) {
  if (typeof str !== 'string') {
    throw new Error("Your input is not a string");
  }
  return str.match(/^\d+$/) !== null;
} 

const str1 = "689454545454";
isOnlyDigitsUsingMatch(str1);  //output: true 

const str2 = "he is 24 years old";
isOnlyDigitsUsingMatch(str2);  //output: false 

Solution 7: Using String.prototype.replace

function isOnlyDigitsUsingReplace(str) {
  if (typeof str !== 'string') {
    throw new Error("Your input is not a string");
  }
  return str.replace(/^\d+$/, '') === '';
} 

const str1 = "689454545454";
isOnlyDigitsUsingReplace(str1);  //output: true 

const str2 = "he is 24 years old";
isOnlyDigitsUsingReplace(str2);  //output: false 

Solution 8: Using Number Conversion

function isOnlyDigitsUsingNumberConversion(str) {
  if (typeof str !== 'string') {
    throw new Error("Your input is not a string");
  }
  return !isNaN(Number(str)) && Number(str).toString() === str;
} 

const str1 = "689454545454";
isOnlyDigitsUsingNumberConversion(str1);  //output: true 

const str2 = "he is 24 years old";
isOnlyDigitsUsingNumberConversion(str2);  //output: false 

Solution 9: Using parseInt

function isOnlyDigitsUsingParseInt(str) {
  if (typeof str !== 'string') {
    throw new Error("Your input is not a string");
  }
  return str === parseInt(str, 10).toString();
} 

const str1 = "689454545454";
isOnlyDigitsUsingParseInt(str1);  //output: true 

const str2 = "he is 24 years old";
isOnlyDigitsUsingParseInt(str2);  //output: false 

Solution 10: Using Set

function isOnlyDigitsUsingSet(str) {
  if (typeof str !== 'string') {
    throw new Error("Your input is not a string");
  }
  const digits = new Set('0123456789');
  return [...str].every(char => digits.has(char));
} 

const str1 = "689454545454";
isOnlyDigitsUsingSet(str1);  //output: true 

const str2 = "he is 24 years old";
isOnlyDigitsUsingSet(str2);  //output: false