Feb 13

Really Simple JavaScript toTitleCase() Implementation

Categories:
Tags:
, ,
Published:
9:46am on Friday 13th February, 2009

Googling for a quick answer to a problem I was having, I noticed today that there are no really simple solutions listed in the search results to a JavaScript implementation of toTitleCase() (the natural partner to the existing String methods toLowerCase() and toUpperCase().

So I made this one.

JavaScript TitleCase method

It’s not incredibly clever, but it does what I needed it to, and will probably be sufficient for your needs 90% of the time. The script extends the JavaScript String type with a new toTitleCase() method, which simply converts all the words in the string to their title-cased (i.e. the first letter is capitalised) equivalent. It effectively ignores words that are already upper-cased, and works fine with strings that include numbers.

String.prototype.toTitleCase = function () {
	var A = this.split(' '), B = [];
	for (var i = 0; A[i] !== undefined; i++) {
		B[B.length] = A[i].substr(0, 1). »
		toUpperCase() + A[i].substr(1);
	}
	return B.join(' ');
}

Include the above snippet in your script somewhere, and you can then call the method in the same way as the other String functions:

var s = "This is a sentence.";
var t = s.toTitleCase();

Neat and simple, and sufficient for most of your JavaScript title-casing needs.

I'd love to hear what you think - please use the form below to leave your comments. Some HTML is permitted: b, i, em, del, ins, strong, pre, code, blockquote, abbr. URLs or email addresses will be automatically converted into links.

Comment Form

  1. FatBusinessman's Gravatar

    FatBusinessman at 1:31pm on 13th February, 2009 #

    How about John Resig’s adaptation of John Gruber’s capitalisation script? It’s available at http://ejohn.org/blog/title-capitalization-in-javascript/.

  2. Matthew Pennell's Gravatar

    Matthew Pennell at 4:08pm on 13th February, 2009 #

    I did say it was only a simple implementation. If you need something that copes with “words I don’t want to capitalise” and other oddities, then there are more complex versions around, but this does the job for me.

  3. Stephane Deschamps's Gravatar

    Stephane Deschamps at 1:39pm on 19th February, 2009 #

    Hi Matthew,

    (such a long time I didn’t utter a thought here, silly work that prevents me from commenting where the web is at)

    I was going to ask a question about usually non-capitalised words, like ‘the’, ‘of’ etc. I see you’ve replied about that.

    So I’m just going to say that I’m lucky to write in french, where capitalisation in titles is the same as in sentences. :)

  4. Cachel's Gravatar

    Cachel at 6:13am on 28th February, 2009 #

    OMG this is cool and yes it is cleaver! Looking at the clean title-casing something crossed my mind that I could do to clean up my JS code and to Capitalize vars plurals… Ta!

  5. Ryan Bateman's Gravatar

    Ryan Bateman at 1:59pm on 20th November, 2009 #

    Great little script - thanks for sharing.