How to useFor loop to titlecase a string in Javascript
In this method, the Javascript For loop is used to iterate over the arguments of the function, and the characters are converted to Uppercase.
Example: In this example, we are using the above-explained method.
function titleCase(str) {
str = str.toLowerCase().split(' ');
for (let i = 0; i < str.length; i++) {
str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1);
}
return str.join(' ');
}
console.log(titleCase("geeks for geeks"));
Output
Geeks For Geeks
Convert string to title case in JavaScript
Converting string to title case means converting to uppercase the first letter of every word in a sentence while the other ones remain in lowercase. In this article, we will see how to convert string to title case in JavaScript
Below are the approaches to convert string to title case in JavaScript:
Table of Content
- Using replace() function
- Using For loop to titlecase a string
- Using map() method
- Using reduce() method
- Using Foreach loop
- Using Regular Expressions and String Split
- Using Lodash’s _.startCase
Contact Us