How to check for uppercase characters in JavaScript
November 4, 2023
JavaScript does not have a built-in 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').
const charCode = 'A'.charCodeAt(0); console.log(charCode); // 65
Using a regular expression
A regular expression can check if a string consists solely of uppercase characters:
const isUpperCase = (str) => /^[A-Z]+$/.test(str); console.log(isUpperCase("HELLO")); // true console.log(isUpperCase("Hello")); // false
Checking individual characters
If you want to check individual characters instead of the whole string:
const isCharUpperCase = (char) => char === char.toUpperCase(); console.log(isCharUpperCase("H")); // true console.log(isCharUpperCase("h")); // false
Iterating over strings
To determine if every character in a string is uppercase, iterate over them:
const isFullyUpperCase = (str) => { for (let i = 0; i < str.length; i++) { if (str[i] !== str[i].toUpperCase()) { return false; } } return true; };
You could ship faster.
Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.
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.
console.log(isCharUpperCase("ß")); // false, even though "ß".toUpperCase() is "SS"
Using locale-specific methods
For locale-aware case conversions, use toLocaleUpperCase
:
const isCharUpperCaseLocale = (char, locale) => char === char.toLocaleUpperCase(locale); console.log(isCharUpperCaseLocale("i", "tr-TR")); // true for Turkish locale
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 "ß".
Performance considerations
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.
TOC
November 4, 2023
JavaScript does not have a built-in 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').
const charCode = 'A'.charCodeAt(0); console.log(charCode); // 65
Using a regular expression
A regular expression can check if a string consists solely of uppercase characters:
const isUpperCase = (str) => /^[A-Z]+$/.test(str); console.log(isUpperCase("HELLO")); // true console.log(isUpperCase("Hello")); // false
Checking individual characters
If you want to check individual characters instead of the whole string:
const isCharUpperCase = (char) => char === char.toUpperCase(); console.log(isCharUpperCase("H")); // true console.log(isCharUpperCase("h")); // false
Iterating over strings
To determine if every character in a string is uppercase, iterate over them:
const isFullyUpperCase = (str) => { for (let i = 0; i < str.length; i++) { if (str[i] !== str[i].toUpperCase()) { return false; } } return true; };
You could ship faster.
Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.
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.
console.log(isCharUpperCase("ß")); // false, even though "ß".toUpperCase() is "SS"
Using locale-specific methods
For locale-aware case conversions, use toLocaleUpperCase
:
const isCharUpperCaseLocale = (char, locale) => char === char.toLocaleUpperCase(locale); console.log(isCharUpperCaseLocale("i", "tr-TR")); // true for Turkish locale
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 "ß".
Performance considerations
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.
November 4, 2023
JavaScript does not have a built-in 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').
const charCode = 'A'.charCodeAt(0); console.log(charCode); // 65
Using a regular expression
A regular expression can check if a string consists solely of uppercase characters:
const isUpperCase = (str) => /^[A-Z]+$/.test(str); console.log(isUpperCase("HELLO")); // true console.log(isUpperCase("Hello")); // false
Checking individual characters
If you want to check individual characters instead of the whole string:
const isCharUpperCase = (char) => char === char.toUpperCase(); console.log(isCharUpperCase("H")); // true console.log(isCharUpperCase("h")); // false
Iterating over strings
To determine if every character in a string is uppercase, iterate over them:
const isFullyUpperCase = (str) => { for (let i = 0; i < str.length; i++) { if (str[i] !== str[i].toUpperCase()) { return false; } } return true; };
You could ship faster.
Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.
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.
console.log(isCharUpperCase("ß")); // false, even though "ß".toUpperCase() is "SS"
Using locale-specific methods
For locale-aware case conversions, use toLocaleUpperCase
:
const isCharUpperCaseLocale = (char, locale) => char === char.toLocaleUpperCase(locale); console.log(isCharUpperCaseLocale("i", "tr-TR")); // true for Turkish locale
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 "ß".
Performance considerations
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.
What is Basedash?
What is Basedash?
What is Basedash?
Ship faster, worry less with Basedash
Ship faster, worry less with Basedash
Ship faster, worry less with Basedash
You're busy enough with product work to be weighed down building, maintaining, scoping and developing internal apps and admin panels. Forget all of that, and give your team the admin panel that you don't have to build. Launch in less time than it takes to run a standup.
You're busy enough with product work to be weighed down building, maintaining, scoping and developing internal apps and admin panels. Forget all of that, and give your team the admin panel that you don't have to build. Launch in less time than it takes to run a standup.
You're busy enough with product work to be weighed down building, maintaining, scoping and developing internal apps and admin panels. Forget all of that, and give your team the admin panel that you don't have to build. Launch in less time than it takes to run a standup.
Dashboards and charts
Edit data, create records, oversee how your product is running without the need to build or manage custom software.
USER CRM
ADMIN PANEL
SQL COMPOSER WITH AI
Related posts
Related posts
Related posts
How to Remove Characters from a String in JavaScript
Jeremy Sarchet
How to Sort Strings in JavaScript
Max Musing
How to Remove Spaces from a String in JavaScript
Jeremy Sarchet
Detecting Prime Numbers in JavaScript
Robert Cooper
How to Parse Boolean Values in JavaScript
Max Musing
How to Remove a Substring from a String in JavaScript
Robert Cooper