How to convert a sentence to a Title case in JavaScript

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");


Javascript Is Very Easy To Learn

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.