//capitalize only the first letter of the string.
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
//capitalize all words of a string.
function capitalizeWords(string) {
return string.replace(/(?:^|s)S/g, function(a) { return a.toUpperCase(); });
};
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 array
let namescap = names.map((x)=>{
return x[0].toUpperCase()+x.slice(1)
})
console.log(namescap)
// this will only capitalize the first word
var name = prompt("What is your name");
firstLetterUpper = name.slice(0,1).toUpperCase();
alert("Hello " + firstLetterUpper + name.slice(1, name.length).toLowerCase());
how to check if the first letter of a string is capitalized or uppercase in js
/**** Check if First Letter Is Upper Case in JavaScript***/
function startsWithCapital(word){
return word.charAt(0) === word.charAt(0).toUpperCase()
}
console.log(startsWithCapital("Hello")) // true
console.log(startsWithCapital("hello")) // false
var name = prompt("What is your name");
firstLetterUpper = name.slice(0,1).toUpperCase();
alert("Hello " + firstLetterUpper + name.slice(1, name.length).toLowerCase());
// program to convert first letter of a string to uppercase
function capitalizeFirstLetter(str) {
// converting first letter to uppercase
const capitalized = str.charAt(0).toUpperCase() + str.slice(1);
return capitalized;
}
// take input
const string = prompt('Enter a string: ');
const result = capitalizeFirstLetter(string);
console.log(result);
Checking if the first letter of the string is uppercase
if (this.state.name[0] >= 'A' && this.state.name[0] <= 'Z')
this.setState({ name: "First letter is uppercase" })
else
this.setState({ name: "First letter is NOT uppercase" })
}