Languages
[Edit]
EN

TypeScript - check if character is digit

0 points
Created by:
Mahir-Bright
1311

In this short article, we would like to show how to check if the character is a digit using TypeScript.

Quick solution:

const DIGIT_EXPRESSION: RegExp = /^\d$/;

const isDigit = (character: string): boolean => {
  return character && DIGIT_EXPRESSION.test(character);
};

 

Practical example

const DIGIT_EXPRESSION: RegExp = /^\d$/;

const isDigit = (character: string): boolean => {
  return character && DIGIT_EXPRESSION.test(character);
};


// Usage example:

// digits

console.log(isDigit('0')); // true
console.log(isDigit('5')); // true
console.log(isDigit('9')); // true

// letters

console.log(isDigit('a')); // false
console.log(isDigit('z')); // false
console.log(isDigit('A')); // false
console.log(isDigit('Z')); // false

// other

console.log(isDigit('10')); // false
console.log(isDigit('ab')); // false
console.log(isDigit('😎')); // false

console.log(isDigit('-')); // false
console.log(isDigit('.')); // false
console.log(isDigit('+')); // false
console.log(isDigit('*')); // false

Alternative titles

  1. TypeScript - isDigit function
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join