W3docs

How to Capitalize the First Letter in a String in JavaScript

Read this JavaScript tutorial and learn the methods and undertake some steps which will help you make the first letter in the string uppercase easily.

To capitalize the first letter in a string is easy if you undertake some steps.

First of all you should get the first letter of the string by setting the charAt() method at 0 index:

Javascript charAt method

javascript— editable

Next, you should make the first letter uppercase with the <kbd class="highlighted">toUpperCase()</kbd> method:

Javascript the first letter uppercase

javascript— editable

Then, you should get the remainder of the string with the help of the slice() method.

Javascript get the remainder of the string slice method

javascript— editable
Info

Use <kbd class="highlighted"> slice(1)</kbd> to start from the second character to the end of the string.

The final step you should take is creating a function accepting a string as only argument and returns the concatenation of the first capitalized letter and the remainder of the string:

Javascript returns the concatenation of the first capitalized letter

javascript— editable

You may also add that function to the String.prototype to use it directly on a string:

Javascript add that function to the String.prototype

javascript— editable

JavaScript Strings

JavaScript strings are used to store and manipulate text. No separate type exists for a single character. The strings internal format is UTF-16. A string represents either zero or more characters that are written inside quotes.

The <kbd class="highlighted">toUpperCase()</kbd> method returns the string value that is converted to uppercase. It does not affect any special character, digits, and alphabets already in uppercase.