Javascript Title Case Function

I’ve been tinkering with VueJS lately and one function that I’ve been using a lot recently is this toTitleCase function. Bring in dynamic content from an API results sometimes in text that I want to be a heading coming through as lowercase. So this little function with change it to title case for me.

function toTitleCase(str) {
  return str.replace(/\w\S*/g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  });
}

toTitleCase("web design");

Result: Web Design

Theres also a little fiddle for this here. See it here