Table of Contents
Understanding the Basics
charAt(): This method returns the character at a specified index in a string. For instance,
string.charAt(0)returns the string's first character.
toUpperCase(): This method converts the entire string to uppercase letters.
slice(): This method extracts a part of a string and returns it as a new string. For example,
string.slice(1)returns the string from the second character to the end.
Extract the First Character: First, we need to get the first character of the string. This can be done using
charAt(0)or by simply accessing the first element of the string like an array,
Convert the First Character to Uppercase: The extracted character is then converted to uppercase using the
Concatenate with the rest of the String: Finally, we concatenate the uppercase character with the rest of the string, which is obtained using the
In this function,
capitalizeFirstLetter, we check whether the string is empty. If not, we process the string as described above.
Things to Keep in Mind
- This function will return an empty string unchanged.