Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

How do I make the first letter of a string uppercase in JavaScript?

How do I make the first character of a string uppercase if it's a letter, but not change the case of any of the other letters?

For example:

  • "this is a test""This is a test"
  • "the Eiffel Tower""The Eiffel Tower"
  • "/index.html""/index.html"

Answer*

Draft saved
Draft discarded
Cancel
11
  • I had been wondering for a while why toUpperCase didn't really do much for some languages... but didn't quite care enough to find out. Glad I finally did, this was a very interesting read! Commented Jul 19, 2021 at 22:09
  • This doesn't seem to work with digraphs such as "IJ" in dutch. Using the latest version the example here is incorrectly capitalized to "Ijsselmeer" (The regex version). The code I used was: capitalizeFirstLetter('ijssel', 'nl-NL') - That's a correct localization string right? Commented Aug 24, 2021 at 11:38
  • 4
    In netherlands dutch IJ is considered 2 letters, which are just capitalized at the same time (in contrary to the belgian version). Commented Jun 23, 2022 at 16:52
  • 3
    Great answer! Quite embarrassing how many broken answers exist here. I would recommend adding a small "tl;dr" section at the very top. Commented Sep 28, 2023 at 7:55
  • 2
    @GéryOgam It may be helpful to know the formal term for what you’re trying to do with the second regular expression in the answer you linked: "segmentation". Segmentation is complex and, in some cases, its behavior varies with language as well. It’s defined by UAC #29. Fortunately, ECMAScript (via ECMA-402, the Intl API) provides an API for this, %Intl.Segmenter%. You’d use granularity: "word". Commented Jul 23, 2024 at 19:19

lang-js

AltStyle によって変換されたページ (->オリジナル) /