HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Check if a string is uppercase or lowercase in JavaScript

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptuppercasechecklowercasestring

Problem

It's not uncommon to need to check if a string is uppercase or lowercase in JavaScript. Fundamentally, this is a very simple task, as we can easily convert any string to uppercase or lowercase. We can then compare the original string to the converted string and get the desired result.
In order to check if a string is uppercase, we can convert the string to uppercase using String.prototype.toUpperCase() and compare it to the original string.
Conversely, we can do the same for lowercase strings, comparing the original string with the output of String.prototype.toLowerCase().
The above examples work well for alphabetic characters, but what about non-alphabetic characters? For example, '!@#$' is neither uppercase nor lowercase, but both isUpperCase('!@#$') and isLowerCase('!@#$') return true.
Regular expressions offer a solution to this problem. Here's a quick rundown of the regular expression syntax we'll be using:

Solution

const isUpperCase = str => str === str.toUpperCase();

isUpperCase('ABC'); // true
isUpperCase('A3@

Conversely, we can do the same for lowercase strings, comparing the original string with the output of String.prototype.toLowerCase().
The above examples work well for alphabetic characters, but what about non-alphabetic characters? For example, '!@#$' is neither uppercase nor lowercase, but both isUpperCase('!@#$') and isLowerCase('!@#$') return true.
Regular expressions offer a solution to this problem. Here's a quick rundown of the regular expression syntax we'll be using:
  • Use the ^ anchor to match the start of the string.
  • Use [a-z] or [A-Z] to match a range of alphabetic characters (case-sensitive).
  • Use a positive lookahead (?=) to ensure that at least one alphabetic character is present in the string.

); // true isUpperCase('aB4'); // false


Conversely, we can do the same for lowercase strings, comparing the original string with the output of String.prototype.toLowerCase().
The above examples work well for alphabetic characters, but what about non-alphabetic characters? For example, '!@#$' is neither uppercase nor lowercase, but both isUpperCase('!@#$') and isLowerCase('!@#$') return true.
Regular expressions offer a solution to this problem. Here's a quick rundown of the regular expression syntax we'll be using:
  • Use the ^ anchor to match the start of the string.
  • Use [a-z] or [A-Z] to match a range of alphabetic characters (case-sensitive).
  • Use a positive lookahead (?=) to ensure that at least one alphabetic character is present in the string.

Code Snippets

const isUpperCase = str => str === str.toUpperCase();

isUpperCase('ABC'); // true
isUpperCase('A3@$'); // true
isUpperCase('aB4'); // false
const isLowerCase = str => str === str.toLowerCase();

isLowerCase('abc'); // true
isLowerCase('a3@$'); // true
isLowerCase('Ab4'); // false
const isUpperCase = str => /^(?=[A-Z])[A-Z\s]+$/.test(str);

isUpperCase('ABC'); // true
isUpperCase('A BC'); // true
isUpperCase('A3@$'); // false
isUpperCase(' '); // false
isUpperCase('!@#$'); // false

const isLowerCase = str => /^(?=[a-z])[a-z\s]+$/.test(str);

isLowerCase('abc'); // true
isLowerCase('a bc'); // true
isLowerCase('a3@$'); // false
isLowerCase(' '); // false
isLowerCase('!@#$'); // false

Context

From 30-seconds-of-code: string-is-uppercase-or-lowercase

Revisions (0)

No revisions yet.