geekskai Logogeekskai
ToolsBlogPricing
Sign in
Sign in
ToolsBlogSign in
Tuesday, September 17, 2024|2.37Mins Read

How to Check for at Least One Capital Letter in JS

Authors
  • avatar
    Name
    Geeks Kai
    Twitter
    @KaiGeeks
Discuss on Twitter • View on GitHub

Tags

javascriptstring-manipulationregexprogramming

Previous Article

How to Check for Infinity in JavaScript

Next Article

CSS :first-child Best Practices with Tailwind CSS Examples
← Back to the blog
geekskai Logo
geekskai

Public tools for developers and creators, plus optional Geekskai Audio Toolkit plans. Built with care.

Popular Tools

SoundCloud DownloaderSoundCloud to MP3SoundCloud to WAVSoundCloud PlaylistSoundCloud Artwork
View All Tools

Resources

BlogAbout MePricingTagsProjectsPrivacy PolicyTerms of ServiceRefund Policy

Connect With Us

mailMail
githubGitHub
twitterTwitter
linkedinLinkedin
© 2026 geekskai • All rights reserved

Quick answer: define what counts as a capital letter

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.

Uppercase is not the same as titlecase

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.

An explicit ASCII loop

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.

Why case-conversion tricks are a different rule

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.

Validate presence without changing the text

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.

References

  • MDN: Unicode character class escapes
  • ECMAScript: regular expressions and text processing