by Sai gowtham

How to title case the string in JavaScript

How to title case the string in JavaScript

JavaScript doesn’t offer any inbuilt methods to capitalize the first letter of the string. Let’s write our function to convert the string to title case.

  1. convert the string to lowerCase.
  2. split the single string into an array of strings.
  3. create an array.
  4. loop through the array of strings and capitalize the first letter in every string.
  5. return the title case string.
function titleCase(str){

   str = str.toLowerCase().split(' ');

   let final = [ ];

    for(let  word of str){
      final.push(word.charAt(0).toUpperCase()+ word.slice(1));
    }

  return final.join(' ')

}


titleCase('this is a title case string');

//"This Is A Title Case String"

There is another way to title Case the string by using the CSS.

.main{
    text-transform:captialize;
}

Happy coding…

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
26,545 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
221,520 students enrolled
44 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
114,575 students enrolled
21 hours of video content
View Course