Created
January 29, 2018 07:42
-
-
Save leohxj/d50a271abef7b9c02eac5eadcce80bc5 to your computer and use it in GitHub Desktop.
Make the first letter of a string uppercase in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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