November 4, 2023
isUpperCase function, but checking whether a string is uppercase is a common task during string manipulation. In this guide we’ll explain how to determine if a character or a string is in uppercase using JS.
Understanding character codes
Each character in a string has an associated Unicode value which can be used to determine its case. For standard English letters, uppercase characters have code points from 65 ('A') to 90 ('Z').
Using a regular expression
A regular expression can check if a string consists solely of uppercase characters:
Checking individual characters
If you want to check individual characters instead of the whole string:
Iterating over strings
To determine if every character in a string is uppercase, iterate over them:
Handling non-English characters
Be cautious with non-English characters.
toUpperCase might not behave as expected with characters that don't have a distinct uppercase form.
Using locale-specific methods
For locale-aware case conversions, use
Edge cases to consider
- Non-alphabetic characters: Decide if you need to treat them as uppercase, lowercase, or neither.
- Empty strings: Define what the return value should be for an empty string input.
- Case conversion exceptions: Some characters don't convert neatly between cases, such as the German "ß".
Regular expressions are convenient but might not be as fast as a loop-based approach for long strings. Always consider the context and the size of the data when choosing your method.
How to Truncate Date in MySQL