Software Engineering

Easy methods to Convert String to Title Case in Javascript

Easy methods to Convert String to Title Case in Javascript
Written by admin


If it’s worthwhile to convert a String to Title Case in Javascript, then you are able to do one of many following:

Choice 1 – Utilizing a for loop

operate titleCase(str) {
  str = str.toLowerCase().break up(' ');
  for (var i = 0; i < str.size; i++)
    str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1); 
  return str.be a part of(' ');
}

console.log(titleCase("that is an instance of some textual content!"));

Output: This Is An Instance Of Some Textual content!

Choice 2 – Utilizing map()

operate titleCase(str) {
  return str.toLowerCase().break up(' ').map(operate(phrase) {
    return (phrase.charAt(0).toUpperCase() + phrase.slice(1));
  }).be a part of(' ');
}

console.log(titleCase("that is an instance of some textual content!"));

Output: This Is An Instance Of Some Textual content!

Choice 3 – Utilizing substitute()

operate titleCase(str) {
  return str.toLowerCase().break up(' ').map(operate(phrase) {
    return phrase.substitute(phrase[0], phrase[0].toUpperCase());
  }).be a part of(' ');
}

console.log(titleCase("that is an instance of some textual content!"));

Output: This Is An Instance Of Some Textual content!

About the author

admin

Leave a Comment