Answers for "how to turn first letter of string into uppercase"

158

javascript uppercase first letter

//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(); });
};
Posted by: Guest on July-22-2019
0

How do I make the first letter of a string uppercase in JavaScript?

//1 using OOP Approach 
Object.defineProperty(String.prototype, 'capitalize', {
  value: function() {
      return this.charAt(0).toUpperCase()+ this.slice(1).toLowerCase();
  },
  enumerable: false
});

function titleCase(str) {
  return str.match(/\w\S*/gi).map(name => name.capitalize()).join(' ');
}
//-------------------------------------------------------------------
//2 using a simple Function 
function titleCase(str) {
  return str.match(/\w\S*/gi).map(name => capitalize(name)).join(' ');
}
function capitalize(string) {
  return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
Posted by: Guest on February-10-2022

Code answers related to "how to turn first letter of string into uppercase"

Code answers related to "Javascript"

Browse Popular Code Answers by Language