Skip to content

Instantly share code, notes, and snippets.

@leohxj
Created January 29, 2018 07:42
Show Gist options
  • Save leohxj/d50a271abef7b9c02eac5eadcce80bc5 to your computer and use it in GitHub Desktop.
Save leohxj/d50a271abef7b9c02eac5eadcce80bc5 to your computer and use it in GitHub Desktop.
Make the first letter of a string uppercase in JavaScript
const capitalizeFirstLetter = s => {
const type = typeof s;
if (type !== 'string') {
throw new Error(`Expected string, instead received ${type}`);
}
const [firstChar, ...remainingChars] = s;
return [firstChar.toUpperCase(), ...remainingChars].join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment