Title case is any text, such as in a title or heading, where the first letter of words is capitalized. So we can achieve this by changing the first element of every word in a sentence to uppercase while leaving the other elements in lowercase.
function toTitleCase(string) {
if (string === null || string === '') {
return '';
}
return string.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
const output = toTitleCase("javascript is very easy to learn");
console.log(output);
Output
Javascript Is Very Easy To Learn