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 Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^