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 str = 'captain picard';
2
3function capitalize(str) {
4 return str.charAt(0).toUpperCase() + str.slice(1);
5}
6
7const caps = str.split(' ').map(capitalize).join(' ');
8caps; // 'Captain Picard'
1const toTitleCase = (phrase) => {
2 return phrase
3 .toLowerCase()
4 .split(' ')
5 .map(word => word.charAt(0).toUpperCase() + word.slice(1))
6 .join(' ');
7};
8
9let result = toTitleCase('maRy hAd a lIttLe LaMb');
10console.log(result);
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"));
1const titleCase = title => title
2 .split(/ /g).map(word =>
3 `${word.substring(0,1).toUpperCase()}${word.substring(1)}`)
4 .join(" ");
1/**** Check if First Letter Is Upper Case in JavaScript***/
2function startsWithCapital(word){
3 return word.charAt(0) === word.charAt(0).toUpperCase()
4}
5
6console.log(startsWithCapital("Hello")) // true
7console.log(startsWithCapital("hello")) // false