How to capitalise the first letter of every word in Javascript

To capitalise the first letter of every word in a given string such as for names or headings the following Javascript function can be used:
function wordCaps(sentence){
    var words = sentence.split(' ');
    for(var w = 0; w < words.length; w++){
        words[w] = words[w].substring(0, 1).toUpperCase() + words[w].substring(1, words[w].length);
    }
    return words.join(' ');
}

It can then be called as:

var title = wordCaps("hello world");

Last updated: 21/03/2012