Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Program to Check if a Character is Vowel or Not
Next article icon

JavaScript - String Contains Only Alphabetic Characters or Not

Last Updated : 16 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Here are several methods to check if a string contains only alphabetic characters in JavaScript

Using Regular Expression (/^[A-Za-z]+$/) - Most USed

The most common approach is to use a regular expression to match only alphabetic characters (both uppercase and lowercase).

JavaScript
let s = "HelloWorld"; let isAlphabetic = /^[A-Za-z]+$/.test(s); console.log(isAlphabetic); 

Output

true

This checks if the string contains only the letters A-Z and a-z. It will return true if all characters are alphabetic and false otherwise.

Using every() Method with Regular Expression

The every() method can be used with a regular expression to iterate over each character and verify it matches [A-Za-z].

JavaScript
let s = "HelloWorld"; let isAlphabetic = Array.from(s).every((char) =>     /[A-Za-z]/.test(char)); console.log(isAlphabetic); 

Output

true

This approach works well for checking each character individually against the pattern.

Using for Loop with charCodeAt()

Using a for loop, you can check each character for alphabets without regular expressions.

JavaScript
let s = "HelloWorld"; let isAlphabetic = true;  for (let i = 0; i < s.length; i++) {     let code = s.charCodeAt(i);     if (!(code >= 65 && code <= 90) && !(code >= 97 && code <= 122)) {         isAlphabetic = false;         break;     } } console.log(isAlphabetic); 

Output

true

Using every() with charCodeAt()

This method uses Array.from() to split the string into characters, then checks each one’s ASCII code.

JavaScript
let s = "HelloWorld"; let isAlphabetic = Array.from(s).every(char => {     let code = char.charCodeAt(0);     return (code >= 65 && code <= 90) || (code >= 97 && code <= 122); }); console.log(isAlphabetic);  

Output

true

Using isNaN() Function

To check if each character is non-numeric, you can use isNaN() along with checking for alphabetic ranges.

JavaScript
let s = "HelloWorld"; let isAlphabetic = Array.from(s).every(     (char) => isNaN(char) && /[A-Za-z]/.test(char) ); console.log(isAlphabetic); 

Output

true

Using filter() with match()

The filter() method can filter out non-alphabetic characters by checking each one with match(), and the length of the result should match the original string length if all are alphabetic.

JavaScript
let s = "HelloWorld"; let isAlphabetic =     s.split("").filter((char) =>          char.match(/[A-Za-z]/)).length === s.length; console.log(isAlphabetic); 

Output

true

The regular expression (/^[A-Za-z]+$/) remains the most efficient approach. However, methods like every() with charCodeAt() or a for loop offer flexibility for character-by-character validation.


Next Article
JavaScript Program to Check if a Character is Vowel or Not
author
kohliami558i
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Geeks Premier League
  • javascript-string
  • JavaScript-DSA
  • JavaScript-Program
  • Geeks Premier League 2023

Similar Reads

  • JavaScript - Check if a String Contains Any Digit Characters
    Here are the various methods to check if a string contains any digital character 1. Using Regular Expressions (RegExp)The most efficient and popular way to check if a string contains digits is by using a regular expression. The pattern \d matches any digit (0-9), and with the test() method, we can e
    4 min read
  • JavaScript - Check If a String Contains any Whitespace Characters
    Here are the various methods to check if a string contains any whitespace characters using JavaScript. 1. Using Regular ExpressionsThe most common and effective way to check for whitespace is by using a regular expression. The \s pattern matches any whitespace character. [GFGTABS] JavaScript const s
    3 min read
  • JavaScript Program to Count Number of Alphabets
    We are going to implement an algorithm by which we can count the number of alphabets present in a given string. A string can consist of numbers, special characters, or alphabets. Examples: Input: Str = "adjfjh23"Output: 6Explanation: Only the last 2 are not alphabets.Input: Str = "n0ji#k$"Output: 4E
    3 min read
  • JavaScript Program to Check if a Character is Vowel or Not
    In this article, we will check if the input character is a vowel or not using JavaScript. Vowel characters are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’. All other characters are not vowels. Examples: Input : 'u'Output : TrueExplanation: 'u' is among the vowel characters family (a,e,i,o,u).Input : 'b'Output : Fal
    4 min read
  • JavaScript - Find if a Character is a Vowel or Consonant
    Here are the different methods to find if a character is a vowel or consonant 1. Using Conditional Statements (If-Else)The most widely used approach is to check the character against vowels (both uppercase and lowercase) using conditional statements (if-else). This method is simple and effective for
    5 min read
  • JavaScript Program to Find Missing Characters to Make a String Pangram
    We have given an input string and we need to find all the characters that are missing from the input string. We have to print all the output in the alphabetic order using JavaScript language. Below we have added the examples for better understanding. Examples: Input : welcome to geeksforgeeksOutput
    7 min read
  • JavaScript Program to Test if Kth Character is Digit in String
    Testing if the Kth character in a string is a digit in JavaScript involves checking the character at the specified index and determining if it is a numeric digit. Examples:Input : test_str = ‘geeks9geeks’, K = 5 Output : True Explanation : 5th idx element is 9, a digit, hence True.Input : test_str =
    5 min read
  • JavaScript Program to Check if Two Strings are Same or Not
    In this article, we are going to implement a JavaScript program to check whether two strings are the same or not. If they are the same then we will return true else we will return false. Examples: Input: str1 = Geeks, str2 = GeeksOutput: True. Strings are the SameInput: str1 = Geeks, str2 = GeekOutp
    4 min read
  • How to Check Case of Letter in JavaScript ?
    There are different ways to check the case of the letters in JavaScript which are explained below comprehensively, one can choose the method that best suits their specific use case and requirement. Table of Content Using the function "toUpperCase()" or "toLowerCase"Using regular expressionUsing ASCI
    3 min read
  • JavaScript Program to Check if a Given String is a Rotation of a Palindrome
    In this article, we will see how to check a given string is a rotation of a palindrome. A palindrome is a string that remains the same when its characters are reversed (e.g., "racecar" is a palindrome). Example: Input: str = "racecar"Output: true // "racecar" is a rotation of a palindrome "racecar"I
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences