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);
1// for lowercase on first letter
2let s = "This is my string"
3let lowerCaseFirst = s.charAt(0).toLowerCase() + s.slice(1)
1function capitalizeFirstLetter(string) {
2 return string.charAt(0).toUpperCase() + string.slice(1);
3}
4
5console.log(capitalizeFirstLetter('foo')); // Foo
1function titleCase(str) {
2 var splitStr = str.toLowerCase().split(' ');
3 for (var i = 0; i < splitStr.length; i++) {
4 // You do not need to check if i is larger than splitStr length, as your for does that for you
5 // Assign it back to the array
6 splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
7 }
8 // Directly return the joined string
9 return splitStr.join(' ');
10}
11
12document.write(titleCase("I'm a little tea pot"));
1let val = ' this is test ';
2val = val.trim();
3val = val.charAt(0).toUpperCase() + val.slice(1);
4console.log("Value => ", val);