Check if a Character is a Letter in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
2 min

banner

# Check if a Character is a Letter in JavaScript

To check if a character is a letter:

  1. Compare the lowercase and the uppercase variants of the character.
  2. If the comparison returns false, then the character is a letter.
index.js
function charIsLetter(char) { if (typeof char !== 'string') { return false; } return char.toLowerCase() !== char.toUpperCase(); } console.log(charIsLetter('a')); // ๐Ÿ‘‰๏ธ true console.log(charIsLetter('!')); // ๐Ÿ‘‰๏ธ false console.log(charIsLetter(' ')); // ๐Ÿ‘‰๏ธ false console.log(charIsLetter(null)); // ๐Ÿ‘‰๏ธ false

check if character is letter in javascript

The code for this article is available on GitHub

If the supplied argument is not of type string, we return false right away.

We compare the lowercase and uppercase variants of the string to check if it is a letter.

If the character is a letter, it must have lowercase and uppercase variants.

This works because characters like punctuation and digits don't have lowercase and uppercase variants.

index.js
// ๐Ÿ‘‡๏ธ true console.log('?'.toLowerCase() === '?'.toUpperCase()); // ๐Ÿ‘‡๏ธ true console.log('1'.toLowerCase() === '1'.toUpperCase()); // ๐Ÿ‘‡๏ธ true console.log(' '.toLowerCase() === ' '.toUpperCase());

Comparing the lowercase and uppercase variants of a character returns false for every letter.

index.js
// ๐Ÿ‘‡๏ธ false console.log('a'.toLowerCase() === 'a'.toUpperCase()); // ๐Ÿ‘‡๏ธ false console.log('ะด'.toLowerCase() === 'ะด'.toUpperCase());

# Check if a Character is a Letter using Regex

Alternatively, you can use the RegExp.test() method.

The test() method will return true if the character is a letter and false otherwise.

index.js
function charIsLetter(char) { if (typeof char !== 'string') { return false; } return /^[a-zA-Z]+$/.test(char); } console.log(charIsLetter('a')); // ๐Ÿ‘‰๏ธ true console.log(charIsLetter('!')); // ๐Ÿ‘‰๏ธ false console.log(charIsLetter(' ')); // ๐Ÿ‘‰๏ธ false console.log(charIsLetter(null)); // ๐Ÿ‘‰๏ธ false

check if character is letter using regex

The code for this article is available on GitHub

The RegExp.test() method returns true if the regular expression is matched in the string and false otherwise.

The forward slashes / / mark the beginning and end of the regular expression.

The caret ^ matches the beginning of the input and the dollar sign $ matches the end of the input.

The part between the square brackets [] is called a character class and matches a range of lowercase a-z and uppercase A-Z letters.

The plus + matches the preceding item (the letter ranges) 1 or more times.

This would also work if you provide 2 or more characters.

index.js
function charIsLetter(char) { if (typeof char !== 'string') { return false; } return /^[a-zA-Z]+$/.test(char); } console.log(charIsLetter('a')); // ๐Ÿ‘‰๏ธ true console.log(charIsLetter('abc')); // ๐Ÿ‘‰๏ธ true

works for 2 or more characters

The code for this article is available on GitHub

If you only want to match a single character, remove the plus + from the regex.

index.js
function charIsLetter(char) { if (typeof char !== 'string') { return false; } return /^[a-zA-Z]$/.test(char); } console.log(charIsLetter('a')); // ๐Ÿ‘‰๏ธ true console.log(charIsLetter('abc')); // ๐Ÿ‘‰๏ธ false console.log(charIsLetter('')); // ๐Ÿ‘‰๏ธ false console.log(charIsLetter(null)); // ๐Ÿ‘‰๏ธ false
You can use the i flag to make the regular expression case-insensitive. This allows you to remove the uppercase range A-Z from the square brackets.
index.js
function charIsLetter(char) { if (typeof char !== 'string') { return false; } return /^[a-z]+$/i.test(char); } console.log(charIsLetter('A')); // ๐Ÿ‘‰๏ธ true console.log(charIsLetter('Abc')); // ๐Ÿ‘‰๏ธ true
The code for this article is available on GitHub

The i flag allows us to do a case-insensitive search and replaces the uppercase A-Z range.

If you ever need help reading a regular expression, check out this regular expression cheat sheet by MDN.

It contains a table with the name and the meaning of each special character with examples.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev