Hi Friends 👋,
Welcome To Infinitbility! ❤️
To check character is a letter or number, JavaScript provides the isNaN()
method just pass your character as a parameter it will check character is NaN ( Not a number ) when it returns false means the character is a number else validate the character with regex code /^[a-zA-Z]+$/
to check is a letter or not.
Today, I’m going to show How do I check if a character is a letter or number in Javascript, here I will use the javascript string isNaN()
method, and regex code
/^[a-zA-Z]+$/` to check if a character is a letter or number.
What is a isNaN()
method?
The isNaN() function determines whether a value is NaN or not. Because coercion inside the isNaN function can be surprising, you may alternatively want to use Number.isNaN().
What is a regex code /^[a-zA-Z]+$/
?
This regex code is a format of string letter, using this regex code we are able to validate character is a letter or not.
Let’s start the today’s tutorial How do you check if a character is a letter or number in JavaScript?
In the following example, we will check whether a character is a number or not using the isNaN()
method when a character is a not a number we will use our regex code to check character is a letter or not.
1// Example of character is a letter2const str1 = "infinitbility";34if(!isNaN(str1)){5 console.log('✅ character is a number')6} else if(/^[a-zA-Z]+$/.test(str1)) {7 console.log('✅ character is a letter')8}910// Example of character is a Number11const str2 = "903";1213if(!isNaN(str2)){14 console.log('✅ character is a number')15} else if(/^[a-zA-Z]+$/.test(str2)) {16 console.log('✅ character is a letter')17}
In the above program, we take example of both if string is a letter or string is a number, let’s check the output.

I hope it’s help you, All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.