JavaScript: Check if String Starts with UpperCase

A general requirement many people encounter while working with javascript strings is to check if the first letter of the string is capitalized. This article demonstrates easy ways to check if the string starts with uppercase using different methods and illustration examples.

Table of Contents:

Check If String Starts with UpperCase using toUpperCase()

Javascript’s toUpperCase() will return the calling string converted to uppercase. If the calling value is not a string, it will be converted.

Example:-

Check if the below strings start with uppercase

  • “javascript is veratile language”;
  • “JAVAScript is veratile LanguaGE”;

Code:-

function checkFirstLetterCapital(_string)
{
return _string[0] === _string[0].toUpperCase();
}
let dummyString1 = "javascript is versatile language";
let dummyString2 = "JAVAScript is versatile LanguaGE";
//usage of the function 
console.log( checkFirstLetterCapital(dummyString1) );
console.log( checkFirstLetterCapital(dummyString2) );

Output:-

false
true

Explanation:-

Here in the above code, we first get the first letter from the string using _string[0], and then the toUpperCase() function is applied on it. If the _string[0] is uppercase, return true. Otherwise, return false.

Check If String Starts with UpperCase using RegEXP and test()

JavasScript’s test() method will execute a search for a match between the regular expression and the specified string. This method takes an argument that is a string against which the regular expression is matched. If the match is found, true is returned. Else, false is returned.

Syntax: test(string)

Example:-

Check if the below strings start with uppercase

  • “javascript is veratile language”;
  • “JAVAScript is veratile LanguaGE”;

Code:-

function checkFirstLetterCapital(_string)
{
return /[A-Z]/.test( _string[0]);
}
let dummyString1 = "javascript is versatile language";
let dummyString2 = "JAVAScript is versatile LanguaGE";
//usage of the function 
console.log( checkFirstLetterCapital(dummyString1) );
console.log( checkFirstLetterCapital(dummyString2) );

Output:-

false
true

Explanation:-

  • Here in the above code, we first get the first letter from the string using _string[0], and then the test() function is used. The regular expression is /[A-Z]/.
  • / and / specifies the start and end of the expression.
  • A-Z determines that only the characters from upper case ‘A’ to uppercase ‘Z’ match the pattern.
  • If the expression is matched, then return true; otherwise, false is returned.

Check If String Starts with UpperCase using ASCII Code

ASCII codes are the numeric value given to the characters and symbols for storing and manipulation by computers.

JavaScript’s charCodeAt() method will return an integer ranging from 0 and 65535 that depicts the UTF-16 code value at the given index.

Example:-

Check if the below strings start with uppercase

  • “javascript is veratile language”;
  • “JAVAScript is veratile LanguaGE”;

Code:-

function checkFirstLetterCapital(_string)
{
return _string.charCodeAt(0) >= 65 && _string.charCodeAt(0) <= 90;    
}
let dummyString1 = "javascript is versatile language";
let dummyString2 = "JAVAScript is versatile LanguaGE";
//usage of the function 
console.log( checkFirstLetterCapital(dummyString1) );
console.log( checkFirstLetterCapital(dummyString2) );

Output:-

false
true

Explanation:-

Here in the above code, we check if the ASCII code of the character placed at 0th index falls between 65 and 90, where 65 and 90 are included. This check is made using the method charCodeAt(0).

The uppercase alphabets ASCII code range: 65 to 90.

Check If String Starts with UpperCase using RegEXP and match()

JavaScript’s match() method will return the result of a string matching against a regular expression. This method takes a regular expression object as a parameter and returns an array of matching results. 

Syntax: match(regExp)

Example:-

Check if the below strings start with uppercase

  • “javascript is veratile language”;
  • “JAVAScript is veratile LanguaGE”;

Code:-

function checkFirstLetterCapital(_string)
{
return _string.match(new RegExp(/^[A-Z]/)) !== null;    
}
let dummyString1 = "javascript is versatile language";
let dummyString2 = "JAVAScript is versatile LanguaGE";
//usage of the function 
console.log( checkFirstLetterCapital(dummyString1) );
console.log( checkFirstLetterCapital(dummyString2) );

Output:-

false
true

Explanation:-

  • The function match() is called by the calling string in the above code. The regular expression  /^[A-Z]/ is passed as an argument to the match() method  . 
  • / and / specify the start and end of expression.
  • ^ specifies the beginning character.
  • A-Z determines that the characters range from capital ‘A’ to capital ‘Z’. 
  • If the first character is capital, then the matching expression is returned. Else, null is returned. 
  • Hence, if something is retrieved, the first character is in uppercase. Otherwise, it is not.

I hope this article helped you check if the first letter is capital in a javascript string. Good Luck !!!

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top