Created
August 25, 2020 18:15
-
-
Save kylelambert101/9676329532388ef9bb22d261fd17487e to your computer and use it in GitHub Desktop.
JavaScript -> Convert a string to titlecase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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