Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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(/wS*/gi).map(name => name.capitalize()).join(' ');
}
//-------------------------------------------------------------------
//2 using a simple Function 
function titleCase(str) {
  return str.match(/wS*/gi).map(name => capitalize(name)).join(' ');
}
function capitalize(string) {
  return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
Source by www.positronx.io #
 
PREVIOUS NEXT
Tagged: #How #I #letter #string #uppercase
ADD COMMENT
Topic
Name
1+5 =