Skip to content

Commit

Permalink
Check if a character is a digit
Browse files Browse the repository at this point in the history
  • Loading branch information
phuocng committed Oct 22, 2021
1 parent 3ee1d0a commit 361f1c1
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions snippets/validator/check-if-a-character-is-a-digit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: Check if a character is a digit
category: Validator
---

**JavaScript version**

```js
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**

```js
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**

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

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

0 comments on commit 361f1c1

Please sign in to comment.