JS Coding Questions Logo
JS Coding Questions
#183💼 Interview💻 Code

How to convert a string to title case with javascript

Advertisement

728x90

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
1function toTitleCase(str) {
2  return str.replace(/\w\S*/g, function (txt) {
3  return txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase();
4  });
5  }
6
7toTitleCase("good morning john"); // Good Morning John

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 12

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
183of476
How to convert a string to title case with javascript | JSCodingQuestions.com