I'm not sure if I did this right, as I am pretty new to JavaScript.
But I want to lowercase any random string text and then capitalize the first letter of each word in that text.
<script>
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function lowerCase(string) {
return string.toLowerCase();
}
</script>
Use the built-in method toUpperCase() on the letter you want to transform to uppercase.
In order to pick the first letter, we have to pass two parameters (0, 1) in the substring() method that denotes the first letter of the string and for capitalizing the first letter, we have invoked the toUpperCase() method. For the rest of the string, we again called the substring() method and pass 1 as a parameter.
The toLowerCase method converts a string to lowercase letters. The toLowerCase() method doesn't take in any parameters. Strings in JavaScript are immutable. The toLowerCase() method converts the string specified into a new one that consists of only lowercase letters and returns that value.
Just change the method to
function capitalizeFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
.toLowerCase()
is appended to the last method call.
This method will make the first character uppercase and convert rest of the string to lowercase. You won't need the second method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With