Check if a Character is a Letter in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Check if a Character is a Letter in JavaScript #

Compare the lowercase and uppercase variants of the character to check if it is a letter, e.g. char.toLowerCase() !== char.toUpperCase();. If the comparison returns false, then the character is a letter, otherwise the character isn't 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

We first 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());

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

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

Alternatively, 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 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
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 A-Z range.

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

Further Reading #

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 ยฉ 2022 Borislav Hadzhiev