isdigit implementation in javascript

The isdigit function in JavaScript is not available by default. However, you can implement it easily using regular expressions. Here's an example implementation:

index.tsx
function isdigit(str) {
  return /^[0-9]+$/.test(str);  // checks if the string contains only digits
}
103 chars
4 lines

This implementation uses the test method of the regular expression object to check if the str parameter consists entirely of digits. If it does, it returns true; otherwise, it returns false.

You can use the isdigit function like this:

index.tsx
console.log(isdigit("123"));  // Output: true
console.log(isdigit("abc"));  // Output: false
93 chars
3 lines

Hope this helps!

related categories

gistlibby LogSnag