Capitalize the First Letter in JavaScript: Unicode and Locale Examples
- Authors

- Name
- Geeks Kai
- @KaiGeeks
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.
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.
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.
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.
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.
| Requirement | Starting point |
|---|---|
| First UTF-16 code unit in a deliberately limited input | charAt(0) with a clear contract |
| First Unicode code point | capitalizeFirstCodePoint() |
| First grapheme with locale-aware uppercase | capitalizeFirstGrapheme() |
| Editorial title case or personal-name formatting | Separate 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.