Check if a Character is a Number using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# Table of Contents

  1. Check if a Character in String is a Number with Regex
  2. Check if a Character in String is a Number using isNaN()

# Check if a Character in String is a Number with Regex

Use the RegExp.test() method to check if a character in a string is a number.

The test() method will return true if the character is a valid number and false otherwise.

index.js
function isNumber(char) { return /^\d$/.test(char); } const str = 'a1'; console.log(isNumber(str[0])); // ๐Ÿ‘‰๏ธ false console.log(isNumber(str[1])); // ๐Ÿ‘‰๏ธ true console.log(isNumber('100')); // ๐Ÿ‘‰๏ธ false console.log(isNumber('')); // ๐Ÿ‘‰๏ธ false console.log(isNumber(undefined)); // false
The code for this article is available on GitHub

We used the RegExp.test() method to test a regular expression against the character.

If the regular expression is matched, the method returns true, otherwise, it returns false.

The forward slashes / / mark the start and end of the regular expression.

The caret ^ matches the beginning of the input and the dollar sign $ matches the end of the input.

The \d character matches any digit from 0 to 9.

If the character we pass to the function is a number, the isNumber() function returns true, otherwise, false is returned.

If you also want to return true for multiple characters if they are numbers, e.g. 100, update the regular expression.

index.js
function isNumber(char) { return /^\d+$/.test(char); } const str = 'a1'; console.log(isNumber(str[0])); // ๐Ÿ‘‰๏ธ false console.log(isNumber(str[1])); // ๐Ÿ‘‰๏ธ true console.log(isNumber('100')); // ๐Ÿ‘‰๏ธ true console.log(isNumber('')); // ๐Ÿ‘‰๏ธ false console.log(isNumber(undefined)); // false
The code for this article is available on GitHub

We added the plus + special character to the regular expression.

The plus + matches the preceding item (a digit) 1 or more times.

In its entirety, the regular expression matches a string that starts with and ends with a digit and contains one or more digits.

Alternatively, you can use the isNan() function.

# Check if a Character in String is a Number using isNaN()

You can also use the isNan() function to check if a character is a number.

The function checks if the provided value is NaN (not a number). If the function returns false, then the character is a valid number.

index.js
function isNumber(char) { if (typeof char !== 'string') { return false; } if (char.trim() === '') { return false; } return !isNaN(char); } const str = 'a1'; console.log(isNumber(str[0])); // ๐Ÿ‘‰๏ธ false console.log(isNumber(str[1])); // ๐Ÿ‘‰๏ธ true console.log(isNumber('123')); // ๐Ÿ‘‰๏ธ true console.log(isNumber('')); // ๐Ÿ‘‰๏ธ false console.log(isNumber(undefined)); // false
The code for this article is available on GitHub

The isNumber() function returns true if the supplied string is a number and false otherwise.

We first check if the value passed to the function is not of type string, in which case we return false straight away.

index.js
if (typeof char !== 'string') { return false; }

Then we check if the provided value is an empty string or contains only whitespace, in which case we also return false.

index.js
if (char.trim() === '') { return false; }

Finally, we use the isNaN() function to check if the provided character is not a number.

The logical NOT (!) operator is used to negate the value returned from the isNaN (is not a number) function.

The isNaN function tries to convert the string to a number and returns true if it fails.

index.js
console.log(isNaN('a')); // ๐Ÿ‘‰๏ธ true console.log(isNaN('')); // ๐Ÿ‘‰๏ธ false console.log(isNaN(' ')); // ๐Ÿ‘‰๏ธ false
This seems quite confusing at first, but the isNaN function converts an empty string or a string containing spaces to a number (0), so it returns false.
index.js
console.log(Number('')); // ๐Ÿ‘‰๏ธ 0 console.log(Number(' ')); // ๐Ÿ‘‰๏ธ 0

This is why we used the trim() method - to trim the string and verify that it's not an empty string.

We know that if the isNaN() function gets called with a string that contains at least 1 character and returns true, then the string is NOT a valid number.

Conversely, if the isNaN function gets called with a string that contains at least 1 character and returns false, then the string is a valid number.

Here are some more examples of calling isNaN() with strings.

index.js
console.log(isNaN('123')); // ๐Ÿ‘‰๏ธ false console.log(isNaN('1.23')); // ๐Ÿ‘‰๏ธ false console.log(isNaN('1,23')); // ๐Ÿ‘‰๏ธ true console.log(isNaN('123test')); // ๐Ÿ‘‰๏ธ true

Which approach you pick is a matter of personal preference. I'd use the RegExp.test() method because I find it more direct and intuitive.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev