Navigate back to the homepage

how to check if the first character of a string is a letter in javascript?

Infinitbility
JavaScript
May 31st, 2022 · 1 min read

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

  1. Take sample string variable examples
  2. Use the charAt() method with index 0 to get first character
  3. Use the regex expression and console whether it’s a letter or not
1// string regex format
2const strFormat = /[a-z]/i;
3
4// Take sample string variable examples
5const str1 = 'Hello world';
6const str2 = '8ly';
7
8// Use the `charAt()` method with index 0 to get first character
9// Use the regex expression and console whether it's a letter or not
10
11// valid case example
12if (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}
17
18// invalid case example
19if (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.

javascript, check if the first character of a string is a letter example
javascript, check if the first character of a string is a letter 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 the first character of a string is a number JavaScript?

Example of check if the first character of a string is a number in javascript with step by step guide.

May 30th, 2022 · 1 min read

How to check if the first character of a string is a specific character in javascript?

Example of check if the first character of a string is a specific character in javascript with step by step guide.

May 30th, 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]