How to check if a Character is a Letter in JavaScript

avatar

Borislav Hadzhiev

Fri Oct 22 20212 min read

banner

Photo by Kevin Young

Check if a Character is a Letter in JavaScript #

To check if a character is a letter, compare the lowercase and uppercase variants of the character, using the toLowerCase() and toUppercase() methods. 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

The first thing we do in our function is check if the provided value has a type of string and return false if it doesn't.

We then compare the lowercase and uppercase variants of the string. 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());

However, for every letter, this comparison would return false.

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

As an alternative approach you can use a regular expression.

Check if a Character is a Letter using Regex #

To check if a character is a letter, call the test() method on the following regular expression - /^[a-zA-Z]+$/. If the character is a letter, the test method will return true, otherwise false will be returned.

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

We used the RegExp.test method to check if a character is a letter.

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

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

The caret ^ matches the beginning of the input and the dollar sign $ - 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
You can also use the i flag to make the regular expression case insensitive. This would allow 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 i flag allows us to do case insensitive search and replaces the uppercase range A-Z.

If you ever need help reading a regular expression, check this regex cheatsheet from MDN out.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee