How to useForeach loop in Javascript
In this approach, the split method is used to split the string into an array of words. Then, the forEach loop iterates over each word. Inside the loop, each word is capitalized by converting the first character to uppercase and the remaining characters to lowercase
Example: In this example, we are using the above-explained method.
const str = "geeks for geeks";
let titleCase = "";
str.split(" ").forEach(word => {
const capitalizedWord = word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
titleCase += capitalizedWord + " ";
});
console.log(titleCase);
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