JavaScript Code


How to check if string contains only uppercase using JavaScript?


Check if string contains only uppercase

Given a string, check if the string contains only uppercase letters using JavaScript.

Solution

To check if a string contains only uppercase letters using JavaScript, compare the original string with that of the uppercase version. If both the strings are equal, then the given string can be said to be only uppercase.

Program

1. Given string is str. Check if string str contains only uppercase letters.

str = "HELLO WORLD";

if ( str === str.toUpperCase() ) {
    console.log('string contains only uppercase');
} else {
    console.log('string does not contain only uppercase');
}

2. Given string is str with some uppercase characters. Check if string str contains only uppercase letters.

str = "Hello World";

if ( str === str.toUpperCase() ) {
    console.log('string contains only uppercase');
} else {
    console.log('string does not contain only uppercase');
}