javascript capitalise first letter code example
Example 1: javascript capitalize words
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function capitalizeWords(string) {
return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
};
Example 2: capitalize first letter javascript
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
console.log(capitalizeFirstLetter('foo bar bag'));
Example 3: javascript capitalize first letter
const lower = 'this is an entirely lowercase string';
const upper = lower.charAt(0).toUpperCase() + lower.substring(1);
Example 4: js capitalize first letter of each word
function capitalize(str) {
const arrOfWords = str.split(" ");
const arrOfWordsCased = [];
for (let i = 0; i < arrOfWords.length; i++) {
const word = arrOfWords[i];
arrOfWordsCased.push(word[0].toUpperCase() + word.slice(1).toLowerCase());
}
return arrOfWordsCased.join(" ");
}
Example 5: javascript capitalize
myString = 'the quick green alligator...';
myString.replace(/^\w/, (c) => c.toUpperCase());
myString = ' the quick green alligator...';
myString.trim().replace(/^\w/, (c) => c.toUpperCase());
Example 6: how to check if the first letter of a string is capitalized or uppercase in js
function startsWithCapital(word){
return word.charAt(0) === word.charAt(0).toUpperCase()
}
console.log(startsWithCapital("Hello"))
console.log(startsWithCapital("hello"))