Capitalize first letter of string in JavaScript


Capitalize first letter of string in JavaScript – It is quite easy and simple to capitalize the first letter of string in Javascript. You can use toUpperCase() function to capitalize the first letter of the string. Here in this tutorial we are going to explain how to capitalize the first letter of the given string using the predefined toUpperCase() function. You can use our online editor to try & edit the example code & see output.


How to Capitalize first letter of string in JavaScript?

Capitalize first letter of string in JavaScript Example:

<script type='text/javascript'>
function capitalizeFirst(str) {
    return str.charAt(0).toUpperCase() + str.slice(1);
}
</script>

Try it »

The above function will accept the string and will format it into capitalize form. You can use this function anywhere to capitalize the string. Just pass the string it will return the capitalized string. If you run the above example it will produce the output something like this –

Capitalize first letter of string in JavaScript


Advertisements

Add Comment

📖 Read More