DEV Community

Discussion on: Capitalize the first letter of every word

anders profile image

This looks like a better version from a performance perspective for sure, I did this implementation of the same

function captilizeAllWords(s) {

  var capitalize = true;
  var returnValue = '';

  for (var i = 0; i < s.length; ++i) {

    var c = s[i];

    if (c == ' ') { 

      capitalize = true; 

    } else if (capitalize) {

      c = c.toUpperCase();
      capitalize = false;

    returnValue += c;

  return returnValue;

In an article I wrote here a few days ago:

Given that "of" seems like it may be a little slow this is probably even faster.