close

How can I capitalize the first letter of each word in a string using JavaScript?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How can I capitalize the first letter of each word in a string using JavaScript? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How can I capitalize the first letter of each word in a string using JavaScript ?

  1. How can I capitalize the first letter of each word in a string using JavaScript ?

    can I capitalize the first letter of each word in a string using JavaScript I usually prefer not to use regexp because of readability and also I try to stay away from loops. I think this is kind of readable.

  2. can I capitalize the first letter of each word in a string using JavaScript

    can I capitalize the first letter of each word in a string using JavaScript I usually prefer not to use regexp because of readability and also I try to stay away from loops. I think this is kind of readable.

Method 1

You are making complex a very easy thing. You can add this in your CSS:

.capitalize {
    text-transform: capitalize;
}

In JavaScript, you can add the class to an element

 document.getElementById("element").className = "capitalize";

Method 2

I usually prefer not to use regexp because of readability and also I try to stay away from loops. I think this is kind of readable.

function capitalizeFirstLetter(string) {
    return string && string.charAt(0).toUpperCase() + string.substring(1);
};

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment