JavaScript Code


How to check if string contains only lowercase using JavaScript?


Check if string contains only lowercase

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

Solution

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

Program

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

str = "hello world";

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

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

str = "Hello World";

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