JavaScript

Capitalize Words (Title Case)

by @admin
10h ago
Apr 28, 2026
Public
Converts a string to title case by capitalising the first letter of each word. Handles multiple spaces and mixed-case input. Optionally skips common English stopwords (a, an, the, of, in, …) so article/preposition words are not capitalised in the middle of a title — matching standard editorial style guides.
JavaScript
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
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.