javascript string capitalize each word code example
Example 1: javascript capitalize first letter of each word
function titleCase(str) {
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
return splitStr.join(' ');
}
document.write(titleCase("I'm a little tea pot"));
Example 2: js capitalize first letter of each word
const titleCase = title => title
.split(/ /g).map(word =>
`${word.substring(0,1).toUpperCase()}${word.substring(1)}`)
.join(" ");