Capitalize First Letter of Each Word
Learn how to capitalize the first letter of every word in a text string. Understand the difference between simple word capitalization and proper Title Case with small-word exceptions.
Detailed Explanation
Capitalizing the First Letter of Each Word
Word capitalization (sometimes called "Start Case") converts the first letter of every word to uppercase, regardless of the word's role in the sentence. Unlike Title Case, it does not apply small-word exceptions.
Basic Conversion
Input: the quick brown fox
Output: The Quick Brown Fox
Input: hello-world example
Output: Hello-World Example
Capitalize Words vs. Title Case
Capitalize Words: The Quick Brown Fox Jumps Over The Lazy Dog
Title Case: The Quick Brown Fox Jumps over the Lazy Dog
Capitalize Words capitalizes every word, including articles ("The"), prepositions ("Over"), and conjunctions. Title Case applies grammatical exceptions.
How It Works
The algorithm is simple:
- Split the text into words (by spaces).
- For each word, uppercase the first character and keep the rest unchanged (or lowercase it).
- Join the words back together.
function capitalizeWords(text) {
return text.replace(/\b\w/g, (char) => char.toUpperCase());
}
Handling Delimiters
A robust implementation capitalizes after multiple types of word boundaries:
"hello world" → "Hello World" (space)
"hello-world" → "Hello-World" (hyphen)
"hello.world" → "Hello.World" (dot)
"hello_world" → "Hello_World" (underscore)
"hello/world" → "Hello/World" (slash)
Common Applications
- Form display names:
"john doe"→"John Doe" - Address formatting:
"123 main street"→"123 Main Street" - CSV data cleanup: Normalizing inconsistently capitalized data
- Product names:
"wireless bluetooth headphones"→"Wireless Bluetooth Headphones" - Report headers: Quick capitalization for generated reports
Preserving Existing Capitals
Some implementations preserve the case of characters after the first:
"iPhone macOS" → "IPhone MacOS" (aggressive — lowercases then capitalizes)
"iPhone macOS" → "IPhone MacOS" (preserving — only touches first char)
Most users expect the first approach, but an option to preserve original casing can be useful.
Edge Cases
- Empty strings return empty strings.
- Single character:
"a"→"A". - Multiple spaces: should be preserved (don't collapse whitespace).
- Numbers as first character:
"2nd place"→"2nd Place"(the number word is skipped).
Use Case
Capitalizing each word is useful for formatting display names in user profiles, normalizing address fields in forms, cleaning up CSV/spreadsheet data, generating report headers, formatting breadcrumb navigation labels, and displaying product names in e-commerce catalogs.