InterviewHubPRO
Section Overview
Q187

Question 187 of 521

Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

36%
JavaScript Internals (521 Qs)•Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

How to convert a string to title case with javascript

Title case means that the first letter of each word is capitalized. You can convert a string to title case using the below function,

javascript
  function toTitleCase(str) {
    return str.replace(/\w\S*/g, function (txt) {
      return txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase();
    });
  }
  toTitleCase("good morning john"); // Good Morning John