Check if a String is in Uppercase or Lowercase in JavaScript
📅March 28, 2022
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
- How to highlight searched text using JavaScript
- How to set an HTML element’s class using JavaScript
- How to create an auto-resize Textarea in JavaScript
- How to get a cookie by name in JavaScript
- Detect a mobile device using 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.
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.
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..!! 🙂