Hi Friends đź‘‹,
Welcome To Infinitbility! ❤️
Today, I’m going to show How do check if the first character of a string is a letter in Javascript, here I will use the javascript string charAt()
method and regex expression to check if the first character of a string is an alphabet or not.
What is a charAt()
method?
The String object’s charAt()
method returns a new string consisting of the single UTF-16 code unit located at the specified offset into the string.
Let’s start the today’s tutorial How do you check if the first character of a string is a number in JavaScript?
Let’s understand how we can do it
- Take sample string variable examples
- Use the
charAt()
method with index 0 to get first character - Use the regex expression and console whether it’s a letter or not
1// string regex format2const strFormat = /[a-z]/i;34// Take sample string variable examples5const str1 = 'Hello world';6const str2 = '8ly';78// Use the `charAt()` method with index 0 to get first character9// Use the regex expression and console whether it's a letter or not1011// valid case example12if (strFormat.test(str1.charAt(0))) {13 console.log("First character is a letter");14} else {15 console.log("First character isn't a letter");16}1718// invalid case example19if (strFormat.test(str2.charAt(0))) {20 console.log("First character is a letter");21} else {22 console.log("First character isn't a letter");23}
The above program is the simplest way to check the string first character of a letter or not. 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.