Saturday, February 7, 2026
HomeLanguagesJavascriptJavaScript Program to Check if a String Contains only Alphabetic Characters

JavaScript Program to Check if a String Contains only Alphabetic Characters

In this article, we are going to learn about checking if a string contains only alphabetic characters in JavaScript.Checking if a string contains only alphabetic characters means verifying that all characters within the string are letters of the alphabet (A-Z or a-z) and do not include any digits, symbols, or other non-alphabetic characters.

There are some common methods that can be used to check if a string contains only alphabetic characters in JavaScript, which are listed below:

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using a Regular Expression

To check if a string contains only alphabetic characters in JavaScript, use the regular expression /^[a-zA-Z]+$/. It returns true if the string consists solely of letters, and false otherwise.

Syntax:

let regex = /^[a-zA-Z]+$/;

Example: In this example, the regular expression ^[a-zA-Z]+$ tests if a string consists only of alphabetic characters (letters). The code validates str1 as true and str2 as false.

Javascript




let str1 = "neveropen";
let str2 = "Geeks123";
  
let regex = /^[a-zA-Z]+$/;
  
console.log(regex.test(str1));
console.log(regex.test(str2));


Output

true
false

Approach 2: Using for of loop

In this approach, we are using for…of loop to iterate through each character in the input string. It returns true if all characters are alphabetic (A-Z or a-z), otherwise false.

Syntax:

for ( variable of iterableObjectName) {
...
};

Example: In this example, checkAlphabets function iterates through each character in the input string and returns true if all characters are alphabetic (A-Z or a-z), otherwise false.

Javascript




function checkAlphabets(input) {
    for (const char of input) {
        if (!(char >= "a" && char <= "z") &&
            !(char >= "A" && char <= "Z")) {
            return false;
        }
    }
    return true;
}
  
const str1 = "neveropen";
const str2 = "Geeks123";
  
console.log(checkAlphabets(str1));
console.log(checkAlphabets(str2));


Output

true
false
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32491 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6863 POSTS0 COMMENTS
Nicole Veronica
11987 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12076 POSTS0 COMMENTS
Shaida Kate Naidoo
6996 POSTS0 COMMENTS
Ted Musemwa
7238 POSTS0 COMMENTS
Thapelo Manthata
6947 POSTS0 COMMENTS
Umr Jansen
6933 POSTS0 COMMENTS