How to usereduce() method in Javascript
The JavaScript arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator. But first we need to convert string into lowercase
Example: In this example, we are using the above-explained method.
function titleCase(st) {
return st.toLowerCase().split(" ").reduce((s, c) =>
s + "" + (c.charAt(0).toUpperCase() + c.slice(1) + " "), '');
}
console.log(titleCase("converting string to titlecase"));
Output
Converting String To Titlecase
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