JavaScript Code


How to check if string starts with uppercase character using JavaScript?


Check if string starts with uppercase

Given a string, check if the string starts with an uppercase letter using JavaScript.

Solution

To check if a string starts with an uppercase letter using JavaScript, get the first character of the string, and check if this character is same as that of the uppercase version of it. If both are equal, then we can say that our string starts with an uppercase letter.

Program

1. Given string is str. Check if string str starts with uppercase letter.

str = "Hello World";

if ( str[0] === str[0].toUpperCase() ) {
    console.log('string starts with uppercase');
} else {
    console.log('string does not start with uppercase');
}