How to Make the First Letter of a String Uppercase in JavaScript?

There are two different ways to make the first letter of a string value using JavaScript. The first method includes using the toUpperCase() along with the slice() method and string concatenation. The second method uses a regular expression in the replace() method.

Both of these methods are going to be demonstrated in this article.

Method 1: Using a combination of toUpperCase() and slice()

First of all, create a new string value and store it inside a variable with the help of the following line:

var string = "hello world!";

After that, treat this string like an array and fetch the character at the zero indexes and use the toUpperCase() function to make it capitalized:


Make sure to store the return value in a separate variable. Otherwise, it won’t work.

var char1 = string.charAt(0).toUpperCase();

After that, verify that the character stored inside the char1 variable is capitalized by passing it to the console log function:


You will see the following result on the terminal:

It is clear from the output that the first character has been extracted and capitalized. But the original string is yet to be restored.

For that, use the slice method with the argument as 1 to slice the string from index 1 to the very end like

var remainingString = string.slice(1);

After that, simply create a new variable and call it as resultString and concatenate char1 and remainingString inside it using the following line:

var resultString = char1 + remainingString;

Pass this variable resultString to the console log function to see the outcome:


Executing the program will produce the following output on the terminal:

The final string has the first letter capitalized.

Method 2: Using regex with replace() method and toUpperCase() method

To demonstrate this, create a new string with the following line:

var string = "google is the most widely used search engine";

Afterwards, define a regex inside a variable for matching the first character of any string

var regExpression = /^./;

Here the pattern /^./ defines the first character of any string value

After that, simply use this regExpression in the replace() method. For the second argument, use the same method of fetching the first character and converting it into the uppercase using the toUpperCase() method like

const finalString = string.replace(regExpression, string[0].toUpperCase());

At the end, pass the finalString variable inside the console log function to get the result on the terminal:


Upon execution, this program will produce the following results:

The first character of the string has been successfully capitalized.

Wrap up

To capitalize the first character of any string value, use combinations of string manipulation functions. You can apply two methods. In the first method, toUpperCase() is used to capitalize the first character after it has been fetched from the string and stored inside a new variable. Afterward, the slice() method is used to fetch the remaining string and concatenate the first character and the capitalized first character. In the second method, a regular expression is used to match the string’s first character, and then toUpperCase() is used to capitalize that character.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.