Check if Letter in String is Uppercase or Lowercase in JS

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
7 min

banner

# Table of Contents

  1. Check if letter in String is Uppercase or Lowercase
  2. Check if letter in String is Uppercase or Lowercase using RegExp.test()
  3. Check if the First Letter of a String is Uppercase in JavaScript
  4. Capitalize the first letter of a string

# Check if letter in String is Uppercase or Lowercase

To check if a letter in a string is uppercase or lowercase:

  1. Use the toUpperCase() method to convert the letter to uppercase.
  2. Compare the letter to itself.
  3. If the comparison returns true, the letter is uppercase, otherwise, it's lowercase.
index.js
const letter = 'A'; // โœ… Check if a letter is uppercase if (letter.toUpperCase() === letter) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… letter is uppercase'); } else { console.log('โ›”๏ธ letter is lowercase'); } // --------------------------------------- // โœ… Check if a letter is lowercase const letter2 = 'b'; if (letter2.toLowerCase() === letter2) { // ๐Ÿ‘‡๏ธ this runs console.log('The letter is lowercase'); } else { console.log('The letter is uppercase'); }

check if letter in string is uppercase or lowercase

The code for this article is available on GitHub

We used the String.toUpperCase() method to convert the character to uppercase so we can compare it.

If comparing the uppercase variant of the letter to the letter itself returns true, then the letter is uppercase.

index.js
const char = 'B'; console.log(char.toUpperCase()); // ๐Ÿ‘‰๏ธ 'B' console.log(char === char.toUpperCase()); // ๐Ÿ‘‰๏ธ true

Otherwise, the letter is lowercase.

index.js
const char = 'b'; console.log(char.toUpperCase()); // ๐Ÿ‘‰๏ธ 'B' console.log(char === char.toUpperCase()); // ๐Ÿ‘‰๏ธ false

You can also access a specific letter in a string by accessing the string at an index.

index.js
const str = 'aBc'; const letter = str[1]; console.log(letter); // ๐Ÿ‘‰๏ธ 'B' if (letter.toUpperCase() === letter) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… letter is uppercase'); } else { console.log('โ›”๏ธ letter is lowercase'); }
The code for this article is available on GitHub
JavaScript indexes are zero-based, so the first character in a string has an index of 0 and the last character has an index of str.length - 1.

If you have to check if a letter is uppercase or lowercase often, define a reusable function.

index.js
function isUpperCase(string) { return string.toUpperCase() === string; } console.log(isUpperCase('ABC')); // ๐Ÿ‘‰๏ธ true console.log(isUpperCase('A')); // ๐Ÿ‘‰๏ธ true console.log(isUpperCase('b')); // ๐Ÿ‘‰๏ธ false console.log(isUpperCase('Ab')); // ๐Ÿ‘‰๏ธ false

The isUpperCase() function takes a string or a single letter and returns true if the letter is uppercase and false otherwise.

The same approach can be used to define an isLowerCase function.

index.js
function isLowerCase(string) { return string.toLowerCase() === string; } console.log(isLowerCase('ABC')); // ๐Ÿ‘‰๏ธ false console.log(isLowerCase('A')); // ๐Ÿ‘‰๏ธ false console.log(isLowerCase('b')); // ๐Ÿ‘‰๏ธ true console.log(isLowerCase('Ab')); // ๐Ÿ‘‰๏ธ false
The code for this article is available on GitHub

The isLowerCase() function takes a string or a single letter as a parameter and returns true if the string is lowercase and false otherwise.

# Dealing with digits and punctuation

However, this approach wouldn't work with digits or punctuation because they can't be uppercase or lowercase.

index.js
const letter = '?'; if (letter.toUpperCase() === letter) { // ๐Ÿ‘‰๏ธ this runs... console.log('โœ… letter is uppercase'); } else { console.log('โ›”๏ธ letter is lowercase'); }

dealing with digits and punctuation

The code for this article is available on GitHub

To resolve this issue, we have to check if the letter has uppercase and lowercase variants.

index.js
const letter = '?'; if (letter.toUpperCase() === letter && letter !== letter.toLowerCase()) { console.log('โœ… letter is uppercase'); } else { // ๐Ÿ‘‰๏ธ this runs console.log('โ›”๏ธ letter is lowercase'); }

There are 2 conditions in our if statement:

  1. Check if the letter's uppercase variant is equal to the letter.
  2. Check if the letter is not equal to its lowercase variant.

If both conditions are met, the letter is uppercase.

We check if the letter has uppercase and lowercase variants because digits and punctuation characters do not.
index.js
const str = '4'; // ๐Ÿ‘‡๏ธ๏ธ true console.log(str.toLowerCase() === str.toUpperCase());

If we know that the letter is uppercase and it's not equal to its lowercase variant, then we have an uppercase letter.

Otherwise, we have a lowercase letter, a digit or punctuation.

If you have to do this often, define reusable functions.

index.js
function isUpperCase(letter) { if ( letter.toUpperCase() === letter && letter !== letter.toLowerCase() ) { console.log('โœ… letter is uppercase'); return true; } console.log('โ›”๏ธ letter is lowercase'); return false; } const str = 'BObby'; console.log(isUpperCase(str[0])); // ๐Ÿ‘‰๏ธ true console.log(isUpperCase(str[1])); // ๐Ÿ‘‰๏ธ true console.log(isUpperCase(str[2])); // ๐Ÿ‘‰๏ธ false console.log(isUpperCase(str)); // ๐Ÿ‘‰๏ธ false
The code for this article is available on GitHub

The isUpperCase function takes a letter and returns true if the letter is uppercase and false otherwise.

You can also pass an entire string to the isUpperCase function, it doesn't have to be a single letter.

The same approach can be used to check if a letter (or a string) is lowercase.

index.js
function isLowerCase(letter) { if ( letter.toLowerCase() === letter && letter !== letter.toUpperCase() ) { return true; } return false; } const str = 'BObby'; console.log(isLowerCase(str[0])); // ๐Ÿ‘‰๏ธ false console.log(isLowerCase(str[1])); // ๐Ÿ‘‰๏ธ false console.log(isLowerCase(str[2])); // ๐Ÿ‘‰๏ธ true console.log(isLowerCase(str)); // ๐Ÿ‘‰๏ธ false

The isLowerCase() function takes a string as a parameter and returns true if the string is lowercase and false otherwise.

# Check if letter in String is Uppercase or Lowercase using RegExp.test()

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

The test() method will return true if the letter is uppercase and false if the letter is lowercase.

index.js
function isUpperCase(string) { return /^[A-Z]+$/.test(string); } console.log(isUpperCase('')); // ๐Ÿ‘‰๏ธ false console.log(isUpperCase('A')); // ๐Ÿ‘‰๏ธ true console.log(isUpperCase('b')); // ๐Ÿ‘‰๏ธ false console.log(isUpperCase('Ab')); // ๐Ÿ‘‰๏ธ false console.log(isUpperCase('ABC')); // ๐Ÿ‘‰๏ธ true

check if letter in string is uppercase or lowercase using regexp test

The code for this article is available on GitHub

If you want to check if a letter is lowercase, you just have to lowercase the A-Z letters in the range.

index.js
function isLowerCase(string) { return /^[a-z]+$/.test(string); } console.log(isLowerCase('')); // ๐Ÿ‘‰๏ธ false console.log(isLowerCase('A')); // ๐Ÿ‘‰๏ธ false console.log(isLowerCase('b')); // ๐Ÿ‘‰๏ธ true console.log(isLowerCase('Ab')); // ๐Ÿ‘‰๏ธ false console.log(isLowerCase('ABC')); // ๐Ÿ‘‰๏ธ false

Note that this approach only works for ASCII letters. If you have to handle all letters, use the approach from the previous subheading.

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

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

The caret ^ symbol 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 the uppercase or lowercase letters in the range.

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

As previously noted, this approach only works for ASCII letters (A-Z).

# Check if the First Letter of a String is Uppercase in JavaScript

To check if the first letter of a string is uppercase:

  1. Use the String.charAt() method to get the first letter of the string.
  2. Use the String.toUppercase() method to convert the letter to uppercase.
  3. Compare the result to the letter.
index.js
function firstIsUppercase(str) { if (str.length === 0) { return false; } return str.charAt(0).toUpperCase() === str.charAt(0); } console.log(firstIsUppercase('Hello')); // ๐Ÿ‘‰๏ธ true console.log(firstIsUppercase('world')); // ๐Ÿ‘‰๏ธ false console.log(firstIsUppercase('')); // ๐Ÿ‘‰๏ธ false if (firstIsUppercase('Hello')) { console.log('โœ… First letter is uppercase'); } else { console.log('โ›”๏ธ First letter is NOT uppercase'); }
The code for this article is available on GitHub

We first check if the supplied string is empty. If it is, the function returns false.

The String.charAt() method returns the character at the specified index or an empty string if the index doesn't exist.

We used the String.toUpperCase() method to convert the first letter of the string to uppercase and then we compared it to itself.

JavaScript indexes are zero-based in JavaScript, so the first character in a string has an index of 0 and the last character has an index of str.length - 1.

If comparing the uppercase variant of the first letter to the letter itself returns true, then the first letter is uppercase.

index.js
const str = 'Hello'; console.log(str[0].toUpperCase()); // ๐Ÿ‘‰๏ธ H console.log(str[0].toUpperCase() === str[0]); // ๐Ÿ‘‰๏ธ true

Otherwise, the first letter is lowercase.

index.js
const str = 'hi'; console.log(str[0].toUpperCase()); // ๐Ÿ‘‰๏ธ H console.log(str[0].toUpperCase() === str[0]); // ๐Ÿ‘‰๏ธ false

# Capitalize the first letter of a string

To capitalize the first letter of a string:

  1. Use the String.charAt method to get the first letter of the string.
  2. Use the String.toUpperCase() method to convert the letter to uppercase.
  3. Use the slice() method to append the rest of the string to the result.
index.js
function capitalizeFirst(str) { return str.charAt(0).toUpperCase() + str.slice(1); } console.log(capitalizeFirst('apple')); // ๐Ÿ‘‰๏ธ Apple console.log(capitalizeFirst('one two')); // ๐Ÿ‘‰๏ธ One two console.log(capitalizeFirst('')); // ๐Ÿ‘‰๏ธ ''
The code for this article is available on GitHub

We used the String.charAt() method to get the first letter of the string and converted it to uppercase.

The last step is to use the String.slice() method to append the remainder of the string to the letter.

index.js
console.log('apple'.slice(1)); // ๐Ÿ‘‰๏ธ pple console.log('hello'.slice(1)); // ๐Ÿ‘‰๏ธ ello

The String.slice() method extracts a section of a string and returns it, without modifying the original string.

The String.slice() method takes the following arguments:

NameDescription
start indexThe index of the first character to include in the returned substring
end indexThe index of the first character to exclude from the returned substring
When only a single argument is passed to the String.slice() method, the slice goes to the end of the string.

The addition (+) operator can be used to concatenate two or more strings.

# Check if the first letter of a string is uppercase using RegExp.test()

You can also use the RegExp.test method to check if the first letter of a string is uppercase.

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

index.js
function firstIsUppercase(str) { return /[A-Z]/.test(str.charAt(0)); } console.log(firstIsUppercase('Hello')); // ๐Ÿ‘‰๏ธ true console.log(firstIsUppercase('world')); // ๐Ÿ‘‰๏ธ false console.log(firstIsUppercase('')); // ๐Ÿ‘‰๏ธ false
The code for this article is available on GitHub

The RegExp.test() method matches a regular expression in a string.

If the regex is matched in the string, the method returns true, otherwise false is returned.

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

The square brackets [] are called a character class and match a range of uppercase letters from A to Z.

We accessed the first character of the string in the call to the test() method.

You can tweak the regular expression to check if the string starts with one or more capital letters.

index.js
function startsWithUppercase(str) { return /^[A-Z]/.test(str); } console.log(startsWithUppercase('Hello')); // ๐Ÿ‘‰๏ธ true console.log(startsWithUppercase('HELLO world')); // ๐Ÿ‘‰๏ธ true console.log(startsWithUppercase('world')); // ๐Ÿ‘‰๏ธ false console.log(startsWithUppercase('')); // ๐Ÿ‘‰๏ธ false

The caret ^ matches the beginning of the input.

Notice that we passed the entire string to the RegExp.test() method.

In its entirety, the regular expression checks if the supplied string starts with one or more uppercase letters.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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