How to Check if the First Letter is Uppercase in JavaScript

How to Check if the First Letter is Uppercase in JavaScript
How to Check if the First Letter is Uppercase in JavaScript

To check if the first letter of a string is uppercase in JavaScript, you can use the following code snippets:

// Method 1: Using Regular Expressions
const firstLetterUppercase = /^[A-Z]/.test(inputString);

// Method 2: Using charAt() and toUpperCase()
const firstLetter = inputString.charAt(0);
const firstLetterUppercase = firstLetter === firstLetter.toUpperCase();

// Method 3: Using Unicode Character Codes
const firstLetterCharCode = inputString.charCodeAt(0);
const isUppercase = firstLetterCharCode >= 65 && firstLetterCharCode <= 90;

When working with strings in JavaScript, it’s often necessary to check if the first letter of a string is uppercase. This can be particularly useful when validating user inputs or formatting data. In this article, we will explore various methods to accomplish this task using JavaScript.

Method 1: Using Regular Expressions

Regular expressions provide a concise and powerful way to match patterns in strings. Let’s see how we can use them to check if the first letter of a string is uppercase.

const inputString = "Hello, World!";

const firstLetterUppercase = /^[A-Z]/.test(inputString);

console.log(firstLetterUppercase); // Output: true

Result:

Explanation:

  • We start by defining a regular expression /^[A-Z]/. The ^ character ensures that we are matching the beginning of the string, and [A-Z] represents any uppercase letter.
  • The test() method is used to check if the input string matches the regular expression. It returns true if the first letter is uppercase, and false otherwise.

Method 2: Using charAt() and toUpperCase()

Another approach to checking if the first letter is uppercase involves using the charAt() and toUpperCase() methods.

const inputString = "Hello, World!";

const firstLetter = inputString.charAt(0);
const firstLetterUppercase = firstLetter === firstLetter.toUpperCase();

console.log(firstLetterUppercase); // Output: true

Result:

Explanation:

  • We use charAt(0) to extract the first character of the input string.
  • Then, we compare the extracted letter with its uppercase version using toUpperCase(). If they are equal, it means the first letter is uppercase.

Method 3: Using Unicode Character Codes

JavaScript assigns a unique Unicode character code to each character. By comparing the character code of the first letter with the character code range of uppercase letters, we can determine if it is uppercase.

const inputString = "Hello, World!";

const firstLetterCharCode = inputString.charCodeAt(0);
const isUppercase = firstLetterCharCode >= 65 && firstLetterCharCode <= 90;

console.log(isUppercase); // Output: true

Result:

Explanation:

  • We use charCodeAt(0) to retrieve the Unicode character code of the first letter.
  • The character codes for uppercase letters range from 65 (‘A’) to 90 (‘Z’). By checking if the first letter’s character code falls within this range, we can determine if it is uppercase.

Conclusion:

In this article, we explored different methods to check if the first letter of a string is uppercase in JavaScript. We learned how to use regular expressions, charAt() and toUpperCase() methods, as well as Unicode character codes. Depending on your specific use case, you can choose the method that suits you best.

Available For New Project

Abdullah Al Imran

I'm Abdullah Al Imran, a Full Stack WordPress Developer ready to take your website to the next level. Whether you're looking for WordPress Theme Development, adding new features, or fixing errors in your existing site, I've got you covered. Don't hesitate to reach out if you need assistance with WordPress, PHP, or JavaScript-related tasks. I'm available for new projects and excited to help you enhance your online presence. Let's chat and bring your website dreams to life!

Leave a Comment

Your email address will not be published. Required fields are marked *