Home / Snippets / How to Capitalize Words in JavaScript
How to Capitalize Words in JavaScript cover

How to Capitalize Words in JavaScript

168

3 years ago

0 comments

To capitalize string in Javascript you can make use of the toUpperCase on the first letter then continue slicing through the string. To ease the process below is the "capitalize" that's attached to the "String prototype" in JavaScript.
/* add string capitalize helper */
Object.defineProperty(String.prototype, 'capitalize', {
    value: function() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    },
    enumerable: false
});

JavaScript Capitalize Helper Function


To make use of the code above you can now call the "capitalize()" function after defining the string.
"hello world".capitalize() // Hello world
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this