Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert a string to title case
/**
* Convert a string to title case.
* https://gist.github.com/kieranbarker/293b74f1b3b46272315d2e1719786b03
* @param {string} str The string to convert.
* @returns {string} The converted string.
*/
function toTitleCase(str) {
return str
.toLowerCase()
.split(" ")
.map(function (word) {
return word.charAt(0).toUpperCase() + word.slice(1);
})
.join(" ");
}
@kieranbarker
Copy link
Author

kieranbarker commented Apr 12, 2021

Please note: this is my adaptation of a function written by Sonya Moisset. All I did was rewrite it in my preferred style using the array map() method instead of a for loop.

@kieranbarker
Copy link
Author

kieranbarker commented Apr 12, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment