geekskai Logogeekskai
ToolsBlogPricing
Sign in
Sign in
ToolsBlogSign in
Tuesday, December 31, 2024|4min read

Capitalize the First Letter in JavaScript: Unicode and Locale Examples

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

Tags

javascriptprogrammingstring-manipulationcoding-tipsperformanceweb-development

Previous Article

JavaScript Sleep Functions: The Developer's Guide to Async Delays That Actually Work

Next Article

EtsiosApp Release Date 2025: The Revolutionary Productivity Platform That's Redefining Work-Life Integration
← 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

Capitalize the first code point

For a string input, uppercase its first Unicode code point and append the unchanged remainder. This handles an initial non-BMP letter without splitting its UTF-16 surrogate pair. It does not find the first alphabetic character later in the string.

function capitalizeFirstCodePoint(text) {
  if (typeof text !== "string") {
    throw new TypeError("Expected a string.")
  }
  if (text.length === 0) {
    return ""
  }
  const firstCodePoint = String.fromCodePoint(text.codePointAt(0))
  return firstCodePoint.toUpperCase() + text.slice(firstCodePoint.length)
}

console.log(capitalizeFirstCodePoint("hello world")) // Hello world
console.log(capitalizeFirstCodePoint("already Good")) // Already Good
console.log(capitalizeFirstCodePoint("")) // empty string
console.log(capitalizeFirstCodePoint("ßeta")) // SSeta: uppercase can expand

The helper returns an empty string for empty input and throws TypeError for non-string input. Leading whitespace or an initial emoji remains first; the function does not skip it or change the rest of the text.

What the common charAt example actually does

text.charAt(0).toUpperCase() + text.slice(1) is adequate for many familiar inputs, including an empty string, but charAt(0) selects one UTF-16 code unit. It is not a general “first visible character” operation. Iterating code points improves surrogate handling, while a multi-code-point grapheme still needs a different boundary.

Use a grapheme boundary and an explicit locale when needed

When your requirement is the first user-perceived character, use a runtime with Intl.Segmenter and select grapheme granularity. Pass the locale intentionally for language-sensitive casing.

function capitalizeFirstGrapheme({ text, locale = "en" }) {
  if (typeof text !== "string") {
    throw new TypeError("Expected a string.")
  }
  if (text.length === 0) {
    return ""
  }
  const segmenter = new Intl.Segmenter(locale, { granularity: "grapheme" })
  const segments = segmenter.segment(text)
  const firstEntry = segments[Symbol.iterator]().next()
  const firstGrapheme = firstEntry.value.segment
  const capitalized = firstGrapheme.toLocaleUpperCase(locale)
  return capitalized + text.slice(firstGrapheme.length)
}

console.log(capitalizeFirstGrapheme({ text: "istanbul", locale: "tr" })) // İstanbul
console.log(capitalizeFirstGrapheme({ text: "istanbul", locale: "en" })) // Istanbul
console.log(capitalizeFirstGrapheme({ text: "e\u0301clair", locale: "fr" })) // E + combining acute + clair
console.log(capitalizeFirstGrapheme({ text: "👨‍👩‍👧‍👦hello", locale: "en" })) // unchanged: first grapheme is an emoji

Feature-check Intl.Segmenter in the target environment or provide a documented compatible implementation. A code-point fallback is a different contract, not equivalent grapheme segmentation. The default locale in this example is explicitly English; change it to the locale required by your application.

Capitalization can change length

Uppercase mapping can expand a character: the first helper changes ßeta to SSeta. Neither helper guarantees a one-character replacement or unchanged output length. The grapheme helper preserves the original remainder; it does not normalize the string or implement every language’s titlecasing conventions.

First-letter uppercase is not universal title case

Capitalizing each ASCII-style word with /\b\w/g does not provide language-independent title case. Word boundaries, punctuation and personal names need rules appropriate to the locale and product. Avoid silently imposing a formatting rule on names supplied by users.

If you format on an input’s blur event, describe it as formatting on loss of focus. It is not a per-keystroke update, and changing a DOM input value does not automatically update a framework’s state.

Choose by the required boundary

RequirementStarting point
First UTF-16 code unit in a deliberately limited inputcharAt(0) with a clear contract
First Unicode code pointcapitalizeFirstCodePoint()
First grapheme with locale-aware uppercasecapitalizeFirstGrapheme()
Editorial title case or personal-name formattingSeparate language/product rules

No benchmark was established here for a “fastest” method. Measure a representative workload only if performance is material. For the separate validation task, see checking whether a string contains a capital letter.

References

  • ECMAScript: string operations and uppercase conversion
  • ECMA-402: Segmenter objects
  • MDN: Intl.Segmenter support and examples