capital each word js 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
function capitalize(str) {
const arrOfWords = str.split(" ");
const arrOfWordsCased = [];
for (let i = 0; i < arrOfWords.length; i++) {
const word = arrOfWords[i];
arrOfWordsCased.push(word[0].toUpperCase() + word.slice(1).toLowerCase());
}
return arrOfWordsCased.join(" ");
}