Check if a String is in Uppercase or Lowercase in JavaScript
Converting a string to uppercase or lowercase is straightforward using the toUpperCase()
or toLowerCase()
methods. But here we will see how to check if a String is all UpperCase or Lowercase using JavaScript.
Checkout more articles on JavaScript
Ways to check if a String is all Uppercase or Lowercase
1. Using regular expression
The Regular Expression is the best way to determine whether a string is in uppercase or lowercase. Use the following code.
1 2 3 4 | const isUpperCase = str => /^[A-Z]*$/.test(str) isUpperCase('CLUE') // true isUpperCase('mediator') // false |
2. Using toUpperCase or toLowerCase methods
We can also use the predefined function toUpperCase() to check the string.
1 2 3 4 5 6 | function isUpperCase (input) { return input === String(input).toUpperCase() } isUpperCase('CLUE') // true isUpperCase('mediator') // false |
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂