capitalize a string

Capitalize the first letter of each word of a given string in JavaScript

How do make the first letter of a string uppercase in JavaScript?

JavaScript offers different ways to capitalize the first letter of each word of a given string.
Let’s see the example for Capitalize the first letter of each word:-
//CAPITALIZE THE FIRST LETTER OF EACH WORD OF A GIVEN STRING
var capitalize = function (string) {
  if (string !== undefined && string.length > 0) {
      var splitStr = string.split(" ");
      for (var i = 0; i < splitStr.length; i++) {
          var j = splitStr[i].charAt(0).toUpperCase();
          splitStr[i] = j + splitStr[i].substr(1).toLowerCase();
      }

      return splitStr.join(" ");
  }
}

Input String - capitalize the first letter of each word
Output String - Capitalize The First Letter Of Each Word

Another example in CSS – For capitalize the first letter of each word i.e.
.capitalize {
  text-transform: capitalize;
}

How to uppercase the first letter of a string in JavaScript?
var capitalize = function (string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

And then:
"hello anil".capitalize();  =>  "Hello anil"

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

Capitalize the first letter of each word of a given string in JavaScript Capitalize the first letter of each word of a given string in JavaScript Reviewed by Anil Singh on 2:38 AM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^