Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to make first letter of a string uppercase in JavaScript ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we will convert the first letter of a string to uppercase in Javascript. There are a number of ways to capitalize the first letter of the string in JavaScript

JavaScript toUpperCase() Function: This function applies on a string and changes all letters to uppercase. 

Syntax:

string.toUpperCase()

JavaScript slice() Function: This function applies to a string and slices it according to the passed parameter. 

Syntax:

string.slice(start, end)

Example: This example uses the slice() method to convert the first letter to uppercase.

Javascript




function capitalizeFLetter() {
    let string = 'geeksforgeeks';
    console.log(string[0].toUpperCase() +
        string.slice(1));
}
capitalizeFLetter()


Output

Geeksforgeeks

JavaScript charAt() Function: This charAt() function returns the character at a given position in the string. 

Syntax:

string.charAt(index)

Example: This example uses charAT() method to make the first letter of a string uppercase.

Javascript




function capitalizeFLetter() {
    let string = 'geeksforgeeks';
    console.log(string.charAt(0).toUpperCase() +
        string.slice(1));
}
capitalizeFLetter()


Output

Geeksforgeeks

JavaScript replace() Function: This is a built-in function in JavaScript that is used to replace a slice of a string with another string or a regular expression. The original string will not be affected. 

Syntax:

str.replace(A, B)

The below examples show the conversion to uppercase using the above-described methods.

Example: This example uses string.replace() method to make the first letter of a string uppercase.

Javascript




function capitalizeFLetter() {
    let string = 'geeksforgeeks';
    console.log(string.replace(/^./, string[0].toUpperCase()));
}
capitalizeFLetter()


Output

Geeksforgeeks

My Personal Notes arrow_drop_up
Last Updated : 02 Jun, 2023
Like Article
Save Article
Similar Reads
Related Tutorials