Capitalize first character of each word in a string in JavaScript

You can convert first character in upper case of each word in a string with using custom written function in JavaScript. Function required a string type parameter and also return a string with first character in upper case of each word. Sample code is mentioned below: Code


<script>
function ucwords(str) {
  return str
    .split(" ")
    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
    .join(" ");
}

// function calling
var greetingStr = "good morning";
alert(ucwords(greetingStr));  // print: Good Morning
</script>