Navigate back to the homepage

How to check if the first letter of a string is uppercase in javascript?

Infinitbility
JavaScript
May 31st, 2022 · 1 min read

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
1function isfirstLetterUppercase(str) {
2 if (typeof str !== 'string' || str.length === 0) {
3 return false;
4 }
5
6 if (str[0].toUpperCase() === str[0]) {
7 return true;
8 }
9
10 return false;
11}
12
13console.log(isfirstLetterUppercase('Hello')); // 👉️ true
14console.log(isfirstLetterUppercase('world')); // 👉️ false
15
16if (isfirstLetterUppercase('Hello')) {
17 console.log('✅ First letter is uppercase')
18} else {
19 console.log('⛔️ First letter is NOT uppercase')
20}

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 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to check if the first character of a string is a letter in javascript?

Example of check if the first character of a string is a letter in javascript with step by step guide.

May 31st, 2022 · 1 min read

How to check if the first character of a string is a number JavaScript?

Example of check if the first character of a string is a number in javascript with step by step guide.

May 30th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]