How to Capitalize first letter in Javascript

Javascript capitalize first letter

In this tutorial, we will see how to Capitalize first letter in Javascript.
[toc]

Capitalize first letter of String in Javascript

There are multiple ways to Capitalize first letter of String in Javascript. Let’s go through each of them.

Using charAt(), toUpperCase() and slice()

We will combination of charAt(), toUpperCase() and slice() functions to capitalize first letter in Javascript.

const str = 'java2blog';
const capitalizeStr = str.charAt(0).toUpperCase() + str.slice(1);
console.log(capitalizeStr);

Output:

Java2blog

Let’s see how it works by going through each method used to capitalize first letter in Javascript.

charAt()

charAt() method returns character at given index in String.
Syntax

const chr = str.charAt(index);

Since we have to capitalize first letter here, we will use str.charAt(0) to extract first letter from the String.

const str = 'java2blog';
const firstChar = str.charAt(0);
console.log(firstChar);

Output:

J

toUpperCase()

toUpperCase() function returns all input character in uppercase.
Syntax

const upperCaseStr = str.toUpperCase();

Since we have to convert first letter to upper case here, we will use str.toUpperCase().

const str = 'java2blog';
const upperCaseStr = str.toUpperCase();
console.log(firstChar);

Output:

JAVA2BLOG

slice()

slice() function returns substring from provided start index until the provided end index.
Syntax

const substr = string.slice(start, end)

If you don’t provide end index, then it will by default returns substring upto end of the String.

const str = 'java2blog';
const substr = str.slice(1);
console.log(substr);

Output:

ava2blog

So we have used str.charAt(0) to extract first letter of String, str.toUpperCase() to capitalize first letter of String and str.slice(1) to concatenate remaining string to the result.

const capitalizeStr = str.charAt(0).toUpperCase() + str.slice(1);

Capitalize each word of String in Javascript

We will now capitalize each word of sentence in Javascript. To do this, we will first split the string into multiple words and store it in array. Iterate over array to apply above capitalize first letter concept to each word and then join them back to form result String.

const str = 'how are you doing';

// Split the array on the basis of blank space
const strArr = str.split(" ");

//iterate through each element of the array and capitalize the first letter of word.

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

// Join all the array elements with space to form sentence again
const capitalizeWordsStr = strArr.join(" ");

console.log(capitalizeWordsStr);

Output:

How Are You Doing

That’s all about how to Capitalize first letter in Javascript.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.