Navigate back to the homepage

How to check if a character is a letter or number in JavaScript?

Infinitbility
JavaScript
June 1st, 2022 · 1 min read

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 letter
2const str1 = "infinitbility";
3
4if(!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}
9
10// Example of character is a Number
11const str2 = "903";
12
13if(!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.

javascript, check if a character is a letter or number example
javascript, check if a character is a letter or number example

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.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to check if a string is valid url in javascript?

Example of check if a string is valid url in javascript with step by step guide.

June 1st, 2022 · 1 min read

How to check if number is integer or float in javascript?

Example of check if number is integer or float in javascript with step by step guide.

June 1st, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]