We can use comparison operators to check if a character is a number using JavaScript. Follow this article to learn how to do it with the explanation and examples below. Let’s go into detail now.
Check If A Character Is A Number Using JavaScript
These are some methods to check if a character is a number using JavaScript.
Use comparison operators
You can check if a character is a number by using comparison operators. You have to compare the character with the character ‘0’ and ‘9’, not the number 0 or 9. The character satisfies the condition is not less than ‘0’ and is not greater than ‘9’.
Look at the example below to learn more about this method.
function isNumber(char){
// if the size of the char is > 1, it is a String, not a Character.
if (char.length > 1) return false
if (char >= '0' && char <= '9') {
return true
}
return false
}
let char1 = 'a'
let char2 = '1'
let char3 = '11'
let char4 = '1a'
console.log(isNumber(char1))
console.log(isNumber(char2))
console.log(isNumber(char3))
console.log(isNumber(char4))
Output
false
true
false
false
Use isNaN() method
The isNaN() method returns true if the specified value is a Number. Otherwise, it returns false.
Syntax:
isNaN(value)
Parameters:
- value: The specified value.
Return Value: true or false.
Look at the example below:
function isNumber(char){
// if the size of the char is > 1, it is a String, not a Character.
if (char.length > 1) return false
if (!isNaN(char)) {
return true
}
return false
}
let char1 = 'a'
let char2 = '1'
let char3 = '11'
let char4 = '1a'
console.log(isNumber(char1))
console.log(isNumber(char2))
console.log(isNumber(char3))
console.log(isNumber(char4))
Output
false
true
false
false
Use the parseInt() method
The parseInt()
method returns an integer if the specified value is an integer. Otherwise, it returns NaN. So you can use isNaN()
method with the parseInt() method inside to check if a character is a number or not.
Take a look in the example below:
function isNumber(char){
// if the size of the char is > 1, it is a String, not a Character.
if (char.length > 1) return false
if (!isNaN(parseInt(char))) {
return true
}
return false
}
let char1 = 'a'
let char2 = '1'
let char3 = '11'
let char4 = '1a'
console.log(isNumber(char1))
console.log(isNumber(char2))
console.log(isNumber(char3))
console.log(isNumber(char4))
Output
false
true
false
false
Like the parseInt() method, you can use the number object or the ‘+’ operator and the isNaN() method to check if a character is a number or not.
You can learn the syntax of using the number object or the ‘+’ operator here.
Summary
To check if a character is a number using JavaScript, you can use the comparison operators, isNaN() method or parseInt() method. Choose the method that is suitable for you. We hope this guide is helpful to you. Thanks!
Maybe you are interested:
- Convert Number to Hexadecimal in JavaScript
- Check if a Number is NaN in JavaScript
- Check if String is a Positive Integer using JavaScript

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R