Skip to main content

Convert a string to title case in JavaScript

  • Convert string to an array of individual words
    • e.g. if you are starting with a string of words separated by spaces, string.split(' ')
    • e.g. if you’re starting from a kebab-case slug, string.split('-')
  • Map through your list of words
  • Capitalize the first words, always
  • Capitalized the last word, always
  • For the inner words, check if they are in a list of words that should not be capitalized (see Further reading below)
    • If they are, leave them as is
    • If they are not, capitalize them
    • Store that list of words as a constant
  • Join the array back into a string
  • Return the string
  • https://apastyle.apa.org/style-grammar-guidelines/capitalization/title-case
    • provides a list of words to keep lowercase according to APA style
    • articles are always lowercase
    • short conjunctions are lowercase, but with 4+ characters they aren’t
    • short prepositions are lowercause, but with 4+ characters they aren’t
    • nouns, pronouns, verbs, adverbs, and adjectives are always capitalized
  • https://www.grammarly.com/blog/capitalization-in-the-titles/
  • see my title-casing logic in root.webc for deriving page titles from slugs when no title has been set