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
* {@link 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

This comment has been minimized.

Copy link
Owner Author

@kieranbarker 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

This comment has been minimized.

Copy link
Owner Author

@kieranbarker kieranbarker commented Apr 12, 2021

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