← coderrocketfuel.com

How To Check If A Character Is A Letter Using JavaScript

While coding in JavaScript, how do you check if a given character is a letter?

In this article, we'll go over a couple of ways to determine this using either a regular expression matching pattern or the ECMAScript case transformation (toLowerCase() and toUpperCase()).

Let's jump straight into the code!

Method 1 - Regular Expression

We'll start with the regular expression method.

A regular expression is a sequence of characters that define a search pattern for parsing and finding matches in a given string.

In the code example below, we use both a /[a-zA-Z]/ regular expression and the test() method to find a match for the given character:

(/[a-zA-Z]/).test(char)

In basic terms, the /[a-zA-Z]/ regex means "match all strings that start with a letter".

If the char matches a value in the regex pattern and, therefore, is safely considered a letter from the alphabet, the test() method will return a true result. If the char value does not match the regular expression, a false value will be returned.

Here is what the method would look like inside a function:

function isCharacterALetter(char) {
  return (/[a-zA-Z]/).test(char)
}

This method will work for characters in the English language, but won't work for other languages like Greek or Latin. Also, this method won't work with letters that have accents and other special characters.

When you test the function, here is what the results would look like:

console.log(isCharacterALetter("t")) // true
console.log(isCharacterALetter("W")) // true
console.log(isCharacterALetter("5")) // false
console.log(isCharacterALetter("β")) // false
console.log(isCharacterALetter("Ф")) // false
console.log(isCharacterALetter("é")) // false

Notice that special characters or other languages beyond English will not work with this method.

In the next example, we'll go over a method that covers a wider range of languages and other non-ASCII Unicode character classes.

Method 2 - ECMAScript Case Transformation

This method will use ECMAScript case transformation (toLowerCase() and toUpperCase()) to check whether or not a given character is a letter.

Here is what the code looks like for this method:

char.toLowerCase() != char.toUpperCase()

Similar to the regular expression method, this will return either a true or false value.

This method works because there are not both uppercase and lowercase versions of punctuation characters, numbers, or any other non-alphabetic characters. In other words, those characters will be identical whether or not they are uppercase or lowercase. On the contrary, letters will be different when you compare their lowercase and uppercase versions.

If you wanted to use this method in a function, here what that would look like:

function isCharacterALetter(char) {
  return char.toLowerCase() != char.toUpperCase()
}

Beyond the English alphabet, this solution will also work for most Greek, Armenian, Cyrillic, and Latin characters. But, it will not work for Chinese or Japanese characters since those languages do not have uppercase and lowercase letters.

When you test the function, the results will look like this:

console.log(isCharacterALetter("t")) // true
console.log(isCharacterALetter("W")) // true
console.log(isCharacterALetter("5")) // false
console.log(isCharacterALetter("β")) // true
console.log(isCharacterALetter("Ф")) // true
console.log(isCharacterALetter("é")) // true

You'll notice that the different languages and other non-ASCII Unicode character classes are covered using this method.

That was the last of the two methods for verifying that a character is a letter.

Thanks for reading and happy coding!