Skip to main content
InterviewHubPRO
Section Overview
Q187

Question 187 of 536

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

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

How to convert a string to title case with javascript

mid LevelMost AskedCode ExampleEvent LoopPrototypes

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