Skip to content
Home » Capitalize the First Letter of a String in JavaScript

Capitalize the First Letter of a String in JavaScript

In this tutorial let’s learn about How to Capitalize the First Letter of a String in JavaScript. In JavaScript, we would utilize three functions to capitalize the first letter of a given string.

  • charAt()
  • toUpperCase()
  • slice()

Capitalize the first letter of a string using charAt() and toUpperCase() Functions

toUpperCase()

toUpperCase() converts all letters in a string to uppercase. To achieve our purpose, we will combine it with additional JavaScript chartAt() and slice() functions.

Code Example :

const str = 'similar geeks';
const final = str.toUpperCase();
console.log(final);

Output :

SIMILAR GEEKS

In the preceding example, we can see that calling the toUpperCase() function on the string transformed all of the characters to capital letters.
To capitalise the first character of a string, we can use the charAt() method to separate the first character and then capitalise it with the toUpperCase() function. Now, we’d use the slice() function to get the string’s remaining characters.

charAt()

The charAt() method returns the character in a string at the given index. The index begins from 0.

Code Example :

const str = 'similar geeks';
const final = str.charAt(0);
console.log(final);

Output :

s

slice()

This function slices a supplied string from a particular “start” place to a given “end” location. The slice(start,end) method returns a section of a string as a new string after extracting a section of it. It is the one of crucial method to Capitalize the First Letter of a String in JavaScript.

Code Example :

const str = 'similar geeks';
const final = str.slice(1);
console.log(final);

Output :

imilar geeks

Let us now combine all three functions to capitalize the first word of an input string.

Code :

const str = 'similar geeks';
const final = str.charAt(0).toUpperCase() + str.slice(1);
console.log(final);

Output :

Similar geeks

Capitalize the first letter of each word of a string in Javascript

To do this, we separate the words from the string and store them in an array, then apply the above technique to each element of the array and connect all of the array components together to recover the string. Let us look at this through the lens of an example.

Code Example :

const str = 'welcome to similar geeks website';

//split the above string into an array of strings
const arr = str.split(" ");

for (var i = 0; i < arr.length; i++) {
    arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);

}

//Join all elements of the array
const final = arr.join(" ");
console.log(final);

Output :

Welcome To Similar Geeks Website

Conclusion

In this tutorial we learn about how to Capitalize the First Letter of a String in JavaScript and also how to Capitalize the first letter of each word of a string in JavaScript.

Similar Posts:

How to Convert a String Into Date in JavaScript
Get the Last Character of a String in JavaScript