Last updated: Mar 3, 2024
Reading timeยท3 min

isNaN()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.
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
We used the RegExp.test() method to test a regular expression against the character.
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.
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
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.
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.
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 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.
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.
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.
console.log(isNaN('a')); // ๐๏ธ true console.log(isNaN('')); // ๐๏ธ false console.log(isNaN(' ')); // ๐๏ธ false
isNaN function converts an empty string or a string containing spaces to a number (0), so it returns false.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.
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.
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.