Capitalize and title-case strings
Uppercase the first letter of a string or of every word, leaving the rest of each word untouched.
- JavaScript
- GreenSnippets
- MIT
- Executed with Node.js 22
const capitalize = (text) => text.charAt(0).toUpperCase() + text.slice(1);
const titleCase = (text) =>
text.toLowerCase().replace(/(^|\s)(\S)/g, (_, space, char) => space + char.toUpperCase());Example / expected output
capitalize("hello world") // "Hello world"
titleCase("hELLO wORLD") // "Hello World"How it works
capitalize only touches the first character. titleCase capitalizes each whitespace-separated word and lowercases the rest. It is a simple rule: it does not know English small words ("of", "the") or acronyms.
Related snippets
- JavaScript Tested
Truncate a string with an ellipsis
Shorten text to a maximum length without cutting in the middle of a word or a Unicode character.
- PHP Tested
Truncate multibyte strings safely in PHP
Shorten UTF-8 text with mb_ functions so accents and emoji are never cut in half.
- Python Tested
Slugify a string
Turn text into a lowercase, hyphenated, ASCII-only slug using only the standard library.
- PHP Tested
Slugify a string in PHP
Create URL-safe lowercase slugs from titles, removing accents and punctuation.

