Skip to content

Capitalize the first letter of every word in a string in JavaScript

Find out how to Capitalize the first letter of every word in a string in JavaScript. In other words, convert the first letter of a string to uppecase using JavaScript.

To capitalize first letter of a string

function capitalize(string) 
{
    return string.charAt(0).toUpperCase() + string.slice(1);
}

To capitalize first letter of each word in a sentence

function capitalize(string) 
{
    string = string.split(" ");

    for (var i = 0, x = string.length; i < x; i++) {
        string[i] = string[i][0].toUpperCase() + string[i].substr(1);
    }

    return string.join(" ");
}
See also  Split an array into half in JavaScript

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.