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

JavascriptWeb DevelopmentObject Oriented Programming

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
raja
Published on 07-Sep-2020 07:33:48
Advertisements