The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,842 other followers

How do I make the first letter of a string uppercase in JavaScript? – Stack Overflow

Posted by jpluimers on 2017/10/11

I’m a JavaScript n00b, so I like solutions like these:

Another solution:

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

You could also add it to the String.prototype so you could chain it with other methods:

String.prototype.capitalizeFirstLetter = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

and use it like this:

'string'.capitalizeFirstLetter() // String

Thanks [WayBackHutch Moore and [WayBackDeviljho for answering at [WayBackHow do I make the first letter of a string uppercase in JavaScript? – Stack Overflow!

Note you can do it in CSS too as explained by [WayBacksam6ber:

In CSS:

p:first-letter {
    text-transform:capitalize;
}

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: