Return String by capitalizing first letter of each word and rest in lower case with JavaScript?


For this, use toUpperCase() along with toLowerCase(). Following is the code −

Example

function capitalEveryFirstletter(subjectTitle) {
   return subjectTitle.split(' ')
   .map(st => st.charAt(0).toUpperCase() + st.slice(1).toLowerCase())
   .join(' ');;
}
var subjectTitle="iNtroduction tO JavaScript ProgRAMMINg";
var output=capitalEveryFirstletter(subjectTitle);
console.log("The result="+output);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo74.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo74.js
The result=Introduction To JavaScript Programming

Updated on: 07-Sep-2020

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements