Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

Today, I’m going to show How do I check if the first letter of a string is uppercase in Javascript, here I will use the javascript string toUpperCase() method and compression operator to check if the first character of a string is an capital or not.

What is a toUpperCase() method?

The toUpperCase() method returns the calling string value converted to uppercase (the value will be converted to a string if it isn’t one).

Let’s start the today’s tutorial How do you check if the first letter of a string is uppercase in Javascript?

To check string’s first letter is uppercase or not, we will convert the first letter to uppercase and compare it with the character if getting true means it’s uppercase else it’s lowercase.

Let’s understand how we can do it

  1. Create a custom function which checks the first character is an uppercase or not
  2. Before checking first letter uppercase validate string is not undefined or empty
  3. validate function with valid and invalid case
function isfirstLetterUppercase(str) {
  if (typeof str !== 'string' || str.length === 0) {
    return false;
  }

  if (str[0].toUpperCase() === str[0]) {
    return true;
  }

  return false;
}

console.log(isfirstLetterUppercase('Hello')); // 👉️ true
console.log(isfirstLetterUppercase('world')); // 👉️ false

if (isfirstLetterUppercase('Hello')) {
  console.log('âś… First letter is uppercase')
} else {
  console.log('⛔️ First letter is NOT uppercase')
}

The above program is the simplest way to check if the first letter of a string is uppercase or not. let’s check the output.

javascript, check if the first letter of a string is uppercase example
javascript, check if the first letter of a string is uppercase example

I hope it’s help you, All the best 👍.