Check if First Letter of String is Uppercase in JavaScript

avatar

Borislav Hadzhiev

Fri Oct 22 20212 min read

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 #

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