How to test if a letter in a string is uppercase or lowercase using javascript?


To test if a letter in a string is uppercase or lowercase using javascript, you can simply convert the char to its respective case and see the result.

Example

function checkCase(ch) {
   if (!isNaN(ch * 1)){
      return 'ch is numeric';
   }
    else {
      if (ch == ch.toUpperCase()) {
         return 'upper case';
      }
      if (ch == ch.toLowerCase()){
         return 'lower case';
      }
   }
}
console.log(checkCase('a'))
console.log(checkCase('A'))
console.log(checkCase('1'))

Output

This will give the output −

lower case
upper case
ch is numeric
raja
Published on 02-Dec-2019 05:59:01
Advertisements