Uppercase and Lowercase: Built-In Methods
"hello world".toUpperCase(); // "HELLO WORLD"
"HELLO WORLD".toLowerCase(); // "hello world"
These two are straightforward — JavaScript strings have built-in methods for both, no manual character handling required.
Sentence Case
function toSentenceCase(str) {
const lower = str.toLowerCase();
return lower.charAt(0).toUpperCase() + lower.slice(1);
}
toSentenceCase("HELLO there WORLD"); // "Hello there world"
Lowercase the whole string first, then capitalize just the very first character.
Simple Title Case (Every Word)
function toTitleCaseSimple(str) {
return str
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}
toTitleCaseSimple("the lord of the rings");
// "The Lord Of The Rings" -- capitalizes EVERY word, including "of"
This simple version capitalizes every word, which is not actually correct title case — as explained in more detail elsewhere, real title case keeps minor words like "of," "the," and "and" lowercase unless they're the first or last word.
Proper Title Case (Respecting Minor Words)
const MINOR_WORDS = new Set(['a', 'an', 'the', 'and', 'but', 'or', 'nor', 'of', 'in', 'on', 'at', 'to', 'for']);
function toProperTitleCase(str) {
const words = str.toLowerCase().split(' ');
return words.map((word, i) => {
const isFirstOrLast = i === 0 || i === words.length - 1;
if (!isFirstOrLast && MINOR_WORDS.has(word)) {
return word;
}
return word.charAt(0).toUpperCase() + word.slice(1);
}).join(' ');
}
toProperTitleCase("the lord of the rings");
// "The Lord of the Rings" -- correct
This version checks each word against a list of minor words, and keeps them lowercase unless they happen to be the first or last word in the string.
Prefer not to write the code yourself?
Open Case Converter