//capitalize only the first letter of the string. functioncapitalizeFirstLetter(string){return string.charAt(0).toUpperCase()+ string.slice(1);}//capitalize all words of a string. functioncapitalizeWords(string){return string.replace(/(?:^|s)S/g,function(a){return a.toUpperCase();});};
constcapitalize=(s)=>{if(typeof s !=='string')return''return s.charAt(0).toUpperCase()+ s.slice(1)}capitalize('flavio')//'Flavio'capitalize('f')//'F'capitalize(0)//''capitalize({})//''
const names =["alice","bob","charlie","danielle"]// --> ["Alice", "Bob", "Charlie", "Danielle"]//Just use some anonymous function and iterate through each of the elements in the array//and take string as another arraylet namescap = names.map((x)=>{return x[0].toUpperCase()+x.slice(1)})console.log(namescap)
// this will only capitalize the first wordvar name =prompt("What is your name");
firstLetterUpper = name.slice(0,1).toUpperCase();alert("Hello "+ firstLetterUpper + name.slice(1, name.length).toLowerCase());
var name =prompt("What is your name");
firstLetterUpper = name.slice(0,1).toUpperCase();alert("Hello "+ firstLetterUpper + name.slice(1, name.length).toLowerCase());
How do I make the first letter of a string uppercase in JavaScript?
//1 using OOP Approach Object.defineProperty(String.prototype,'capitalize',{value:function(){returnthis.charAt(0).toUpperCase()+this.slice(1).toLowerCase();},enumerable:false});functiontitleCase(str){return str.match(/wS*/gi).map(name=> name.capitalize()).join(' ');}//-------------------------------------------------------------------//2 using a simple Function functiontitleCase(str){return str.match(/wS*/gi).map(name=>capitalize(name)).join(' ');}functioncapitalize(string){return string.charAt(0).toUpperCase()+ string.slice(1).toLowerCase();}
// name er first letter uppercase let name ="aminul islam rasel"//Aminul Islam Rasellet word = name.split(" ")// create array each word
word[0]= word[0].charAt(0).toUpperCase()+ word[0].slice(1)
word[1]= word[1].charAt(0).toUpperCase()+ word[1].slice(1)
word[2]= word[2].charAt(0).toUpperCase()+ word[2].slice(1)
name = word.join(" ")// get Aminul Islam Raselconsole.log(name);//show console// array map method use korelet name ="abdur razzak hassan tusher mafus ulla"let word = name.split(" ")
word = word.map(function(value){return value.charAt(0).toUpperCase()+ value.slice(1)})
name = word.join(" ")console.log(name);