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

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: 2020-09-07T07:34:41+05:30

244 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements