1//capitalize only the first letter of the string.
2function capitalizeFirstLetter(string) {
3 return string.charAt(0).toUpperCase() + string.slice(1);
4}
5//capitalize all words of a string.
6function capitalizeWords(string) {
7 return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
8};
1const lower = 'this is an entirely lowercase string';
2const upper = lower.charAt(0).toUpperCase() + lower.substring(1);
1const capitalize = (s) => {
2 if (typeof s !== 'string') return ''
3 return s.charAt(0).toUpperCase() + s.slice(1)
4}
5
6capitalize('flavio') //'Flavio'
7capitalize('f') //'F'
8capitalize(0) //''
9capitalize({}) //''
10
1String.prototype.capitalize = function() {
2 return this.charAt(0).toUpperCase() + this.slice(1)
3}
4