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"