Languages
[Edit]
EN

JavaScript - check if character is digit

8 points
Created by:
Kate_C
23330

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

Quick solution:

const DIGIT_EXPRESSION = /^\d$/;

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

 

Practical example

// ONLINE-RUNNER:browser;

const DIGIT_EXPRESSION = /^\d$/;

const isDigit = (character) => {
    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
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