Check if First Letter of String is Uppercase in JavaScript

avatar

Borislav Hadzhiev

Last updated: Oct 22, 2021

banner

Photo from Unsplash

Check if First Letter of String is Uppercase #

To check if the first letter of a string is uppercase, call the toUppercase() method to convert the first letter to uppercase and compare it to itself. If the comparison returns true, then the first letter is uppercase.

index.js
function firstIsUppercase(str) { if (typeof str !== 'string' || str.length === 0) { return false; } if (str[0].toUpperCase() === str[0]) { return true; } return false; } console.log(firstIsUppercase('Hello')); // 👉️ true console.log(firstIsUppercase('world')); // 👉️ false if (firstIsUppercase('Hello')) { console.log('✅ First letter is uppercase') } else { console.log('⛔️ First letter is NOT uppercase') }

We created a reusable function. The first thing we do is check if the provided parameter is not of type string or is an empty string - in either case, we return false.

We used the String.toUpperCase method to convert the first letter of the string to uppercase and compare it to itself.

If the equality check is successful, our function returns true, otherwise we return false.

Indexes are zero-based in JavaScript, meaning the first character in a string has an index of 0 and the last - 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
However, this approach wouldn't work if the first letter is a digit or punctuation because they can't be uppercase or lowercase.
index.js
const str = '100 percent'; console.log(str[0].toUpperCase()); // 👉️ 1 console.log(str[0].toUpperCase() === str[0]); // 👉️ true

To solve this problem, we have to check if the first letter has uppercase and lowercase variants.

index.js
function firstIsUppercase(str) { if (typeof str !== 'string' || str.length === 0) { return false; } const firstLetter = str[0]; if ( firstLetter.toUpperCase() === firstLetter && firstLetter !== firstLetter.toLowerCase() ) { return true; } return false; } console.log(firstIsUppercase('Hello')); // 👉️ true console.log(firstIsUppercase('100 percent')); // 👉️ false

We have 2 conditions in our if statement:

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

Both conditions have to be met for us to conclude it is an uppercase letter.

We basically check if the first letter has uppercase and lowercase variants at all because digits and punctuation characters do not.

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

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

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.