To capitalize the first letter of a string in JavaScript, uppercase the first character and add the rest of the string: str.charAt(0).toUpperCase() + str.slice(1). This is the standard way to capitalize the first letter in JavaScript, because strings are immutable and have no built-in capitalize() method. This guide covers the one-liner, empty strings, Unicode, and capitalizing every word.
You will need this for names, titles and labels in a user interface. The core idea is small, but the edge cases are what make a good helper function.
Take the first character, convert it to uppercase, then append everything after it.
Here is what each part does:
charAt(0) returns the first character, or an empty string when the string is empty.toUpperCase() converts that character to uppercase.slice(1) returns the string from index 1 to the end, without the first character.The original string is not changed. Strings are immutable, so you always get a new one.
Wrap the logic in a function so you can reuse and test it.
The guard returns the input unchanged for an empty string or a non-string value, so the function never throws on undefined inside a pipeline. Note that only the first letter changes. The rest of the text keeps its case, so capitalize("hELLO") returns "HELLO".
You may see str[0].toUpperCase() + str.slice(1). It works for non-empty strings, but str[0] is undefined on an empty string, and calling toUpperCase() on it throws a TypeError.
Optional chaining fixes it: str[0]?.toUpperCase() ?? "". Using charAt(0) is simpler because it already returns an empty string.
If you want a consistent result such as "John" from "jOHN", lowercase the remainder.
This is useful for normalising names typed in all capitals. Be careful with names such as McDonald or van der Berg, where lowercasing the rest loses information. Real name handling should not be forced into one rule.
To capitalize each word, split on spaces, capitalize each piece and join again. A regular expression can do it in one step.
The pattern \b\p{L} matches a letter at the start of a word. Use the split version for clarity and the regex version when you need Unicode letters handled in one pass.
toUpperCase() follows Unicode rules, so "ß".toUpperCase() returns "SS", which is two characters. For languages with special rules, use toLocaleUpperCase("tr"), because Turkish maps i to İ.
Characters outside the Basic Multilingual Plane use two code units, so charAt(0) would return half of one. When the first character may be an emoji or a rare script, take it with the spread operator: const [first, ...rest] = str.
No. Strings have toUpperCase() and toLowerCase(), but nothing that changes only the first letter. You combine charAt, toUpperCase and slice.
An empty string has no index 0, so str[0] is undefined. Use charAt(0) or optional chaining to avoid the error.
For display only, use text-transform: capitalize on the element. It does not change the stored string, so use JavaScript when the data itself must change.
No. Changing built-in prototypes can clash with future language features and other libraries. Write a standalone function.
Capitalize the first letter of a string with charAt(0).toUpperCase() + slice(1), and guard against empty input. Lowercase the rest when you need sentence case, use toLocaleUpperCase for locale-specific rules, and map over words to capitalize a whole sentence.