Table of Contents
- Capitalize the first letter of a string
- Capitalize the first letter of each word in a string
- Other Related Concepts
charAt():The charAt() function returns the character at a given position in a string.
toUpperCase()The toUpperCase() function converts all the characters of an input string to uppercase
In the above example, we see that using the toUpperCase() function on the string converted all the characters of the input string to capital letters.
But this is not what we desire to achieve. To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it. Now, we would get the remaining characters of the string using the slice() function.
sliceThis function slices a given string from a specified “start” position until the specified “end” position.
Now let us use all the three functions together to capitalize the first word of an input string.
As you can see from the above outputs that we have capitalized the first letter of the input string. Now what if we want to capitalize the first letters of all the words in a string? Let’s see how we can achieve this as well.