JavaScript Capitalize First Letter of a String
Make the first letter of a string uppercase in JavaScript.
The basic solution is:
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() string.slice(1);
}
console.log(capitalizeFirstLetter('foo')); // Foo
Here's a more object-oriented approach:
Object.defineProperty(String.prototype, 'capitalize', {
value: function() {
return this.charAt(0).toUpperCase() this.slice(1);
},
enumerable: false
});
You'd call the function, like this:
"hello, world!".capitalize();
With the expected output being:
"Hello, world!"
p::first-letter {
text-transform:capitalize;
}

Md Obydullah
https://shouts.dev/obydul
Comments
No comments yet…