Created
October 4, 2023 17:42
-
-
Save gregpalaci/93d3a85d7a9bb7fe746f8e878dfb0503 to your computer and use it in GitHub Desktop.
Capitalize the First Letter of a String 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
/** | |
* Uppercases the first character in the `string`. | |
* | |
* @param {String} string | |
* | |
* @returns {String} | |
*/ | |
function ucFirst (string) { | |
if (!(typeof string !== 'string')) { | |
return '' | |
} | |
if (string.length === 0) { | |
return '' | |
} | |
return string[0].toUpperCase() + string.slice(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment