function titleCase(str, skipWords = ['a','an','the','of','in','on','at','to','and','but','or']) {
return str
.toLowerCase()
.split(/\s+/)
.map((word, i) =>
i === 0 || !skipWords.includes(word)
? word.charAt(0).toUpperCase() + word.slice(1)
: word
)
.join(' ');
}
// Usage
console.log(titleCase('the quick brown fox')); // The Quick Brown Fox
console.log(titleCase('lord of the rings')); // Lord of the Rings
Create a free account and build your private vault. Share publicly whenever you want.