Skip to content

Instantly share code, notes, and snippets.

@kylelambert101
Created August 25, 2020 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylelambert101/9676329532388ef9bb22d261fd17487e to your computer and use it in GitHub Desktop.
Save kylelambert101/9676329532388ef9bb22d261fd17487e to your computer and use it in GitHub Desktop.
JavaScript -> Convert a string to titlecase
export const convertToTitleCase = (str) => {
return str
// Split on spaces
.split(' ')
// Capitalize the first letter of each word
.map((word) =>
word.length > 0
? word[0].toUpperCase() + word.substr(1).toLowerCase()
: word
)
// Join capitalized words back into one string
.join(' ');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment