How to Check for at Least One Capital Letter in JS
- Authors

- Name
- Geeks Kai
- @KaiGeeks
Use /[A-Z]/.test(text) for ASCII A–Z. Use /\p{Lu}/u.test(text) for Unicode’s Uppercase_Letter category. Those are different requirements: École contains a Unicode uppercase letter but no ASCII capital.
function hasAsciiCapital(text) {
if (typeof text !== "string") {
throw new TypeError("Expected a string.")
}
return /[A-Z]/.test(text)
}
function hasUnicodeCapital(text) {
if (typeof text !== "string") {
throw new TypeError("Expected a string.")
}
return /\p{Lu}/u.test(text)
}
console.log(hasAsciiCapital("hello JavaScript")) // true
console.log(hasAsciiCapital("hello javascript")) // false
console.log(hasAsciiCapital("École")) // false
console.log(hasUnicodeCapital("École")) // true
console.log(hasUnicodeCapital("Dž")) // false: titlecase (Lt), not uppercase-letter (Lu)
console.log(/[\p{Lu}\p{Lt}]/u.test("Dž")) // true: explicitly include titlecase
console.log(hasUnicodeCapital("123!?")) // false
console.log(hasUnicodeCapital("")) // false
These helpers require a string and throw TypeError for another input type. Empty strings, digits and punctuation do not count as capital letters. The Unicode example needs a JavaScript runtime with Unicode property escapes; do not remove the u flag and expect the same interpretation.
Lu is a specific General_Category value, not an informal promise to recognize every character someone might call a capital. The titlecase letter Dž is Lt; the combined character class in the example includes it explicitly. The broader Unicode Uppercase property is another possible policy and is not identical to Lu. Choose the property your application requires.
Do not add the i flag to a test that must distinguish uppercase from lowercase. For repeatable boolean validation, also avoid g and y: reused regular expressions with those flags update lastIndex when test() runs.
The following loop is a valid alternative when the rule is specifically A–Z. It does not claim to classify uppercase letters in every writing system.
function hasAsciiCapitalByLoop(text) {
if (typeof text !== "string") {
throw new TypeError("Expected a string.")
}
for (const character of text) {
if (character >= "A" && character <= "Z") {
return true
}
}
return false
}
console.log(hasAsciiCapitalByLoop("hello World")) // true
console.log(hasAsciiCapitalByLoop("hello")) // false
Use match() when you need matched text or match details; for a yes/no result, test() expresses the task directly. Neither this distinction nor a short example establishes a universal performance winner.
Comparing character === character.toUpperCase() also accepts characters with no case, such as digits, unless additional conditions are used. Combining uppercase/lowercase comparisons still does not define Unicode’s Lu category. split("") additionally splits UTF-16 code units and can separate a non-BMP character. Prefer a documented character category over an accidental approximation.
These functions inspect the input. They do not uppercase it, normalize it, trim it or enforce a complete password policy. If the next task is converting the beginning of a string, see the separate first-letter capitalization guide.