Skip to content

Instantly share code, notes, and snippets.

@joellesenne
Created September 3, 2020 06:14
Show Gist options
  • Save joellesenne/ff2bf622ec930068135ff1736a37e521 to your computer and use it in GitHub Desktop.
Save joellesenne/ff2bf622ec930068135ff1736a37e521 to your computer and use it in GitHub Desktop.
Capitalize the First Letter of a String in JavaScript
// Capitalize first letter
undefined = true;
((window,document,undefined) => {
const capitalize = word => {
var upperCase = word.toUpperCase().substring(0,1)
var lowerCase = word.toLowerCase().substring(1, word.length);
return `${upperCase}${lowerCase}`;
}
// sample usage
console.log(capitalize("john")); // output John
console.log(capitalize("BRAVO")); // output Bravo
console.log(capitalize("BLAne")); // output Blane
})(window,document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment