Posts Learn Components Snippets Categories Tags Tools About
/

How to Capitalize Words in JavaScript

Learn how to capitalize word in JavaScript the easy way. JavaScript capitalize code snippet and helper

Created on Oct 23, 2021

156 views

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

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

Load comments for How to Capitalize Words in JavaScript

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components

Sponsors 👑

+ Add Yours
)