Skip to content

Instantly share code, notes, and snippets.

@kieranbarker
Last active May 15, 2022 15:32
Show Gist options
  • Save kieranbarker/293b74f1b3b46272315d2e1719786b03 to your computer and use it in GitHub Desktop.
Save kieranbarker/293b74f1b3b46272315d2e1719786b03 to your computer and use it in GitHub Desktop.
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

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

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