JavaScript: Check if String Starts with LowerCase

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

Table of Contents:

Check If String Starts with LowerCase using toUpperCase()

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

Example:-

Check if the below strings start with lowercase

Advertisements
  • “hello this is javaScript”;
  • “HELLO THIS IS JAVASCRIPT”;

Code:-

function checkFirstLetterLowerCase(_string)
{
return _string[0] === _string[0].toLowerCase();
}
let dummyString1 = "hello this is javaScript";
let dummyString2 = "HELLO THIS IS JAVASCRIPT";

console.log( checkFirstLetterLowerCase(dummyString1) );
console.log( checkFirstLetterLowerCase(dummyString2) );

Output:-

true
false

Explanation:-

Here in the above code, we first get the first letter from the string using _string[0], and then the toLowerCase() function is applied to it. Return true if the _string[0] is a lowercase character. Otherwise, return false.

Check If String Starts with LowerCase 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 lowercase

  • “hello this is javaScript”;
  • “HELLO THIS IS JAVASCRIPT”;

Code:-

function checkFirstLetterLowerCase(_string)
{
return /[a-z]/.test( _string[0]);
}
let dummyString1 = "hello this is javaScript";
let dummyString2 = "HELLO THIS IS JAVASCRIPT";

console.log( checkFirstLetterLowerCase(dummyString1) );
console.log( checkFirstLetterLowerCase(dummyString2) );

Output:-

true
false

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 lower case ‘a’ to lowercase ‘z’ match the pattern.
  • If the expression is matched, then return true; otherwise, false is returned.

Check If String Starts with LowerCase 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 lowercase

  • “hello this is javaScript”;
  • “HELLO THIS IS JAVASCRIPT”;

Code:-

function checkFirstLetterLowerCase(_string)
{
return _string.charCodeAt(0) >= 97 && _string.charCodeAt(0) <= 122;    
}
let dummyString1 = "hello this is javaScript";
let dummyString2 = "HELLO THIS IS JAVASCRIPT";

console.log( checkFirstLetterLowerCase(dummyString1) );
console.log( checkFirstLetterLowerCase(dummyString2) );

Output:-

true
false

Explanation:-

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

The lowercase alphabets ASCII code range: 97 to 122.

Check If String Starts with LowerCase 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. 

Example:-

Check if the below strings start with lowercase

  • “hello this is javaScript”;
  • “HELLO THIS IS JAVASCRIPT”;

Code:-

function checkFirstLetterLowerCase(_string)
{
return _string.match(new RegExp(/^[a-z]/)) !== null;    
}
let dummyString1 = "hello this is javaScript";
let dummyString2 = "HELLO THIS IS JAVASCRIPT";
console.log( checkFirstLetterLowerCase(dummyString1) );
console.log( checkFirstLetterLowerCase(dummyString2) );

Output:-

true
false

Explanation:-

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

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

Advertisements

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