Skip to content

Latest commit

 

History

History
41 lines (30 loc) · 852 Bytes

check-if-a-character-is-a-digit.mdx

File metadata and controls

41 lines (30 loc) · 852 Bytes
category created title
Validator
2021-10-22
Check if a character is a digit

JavaScript version

const isDigit = (char) => char < 10;

// Or
const isDigit = (char) => char.length === 1 && c >= '0' && c <= '9';

// Or
const isDigit = (char) => Boolean([true, true, true, true, true, true, true, true, true, true][char]);

TypeScript version

const isDigit = (char: string): boolean => char < 10;

// Or
const isDigit = (char: string): boolean => char.length === 1 && c >= '0' && c <= '9';

// Or
const isDigit = (char: string): boolean => Boolean([true, true, true, true, true, true, true, true, true, true][char]);

Examples

isDigit('a'); // false
isDigit('abc'); // false
isDigit(10); // false
isDigit('10'); // false

isDigit('2'); // true
isDigit(2); // true