Capitalize the first character of each word in a sentence

function capitalizeFirstLetter(str) {
  var words = str.split(" ");
   var i;
  for(i = 0; i <words.length; i ++){

    //substr first value choses the index and the second how many chars it should take
    words[i] = words[i].substr(0, 1).toUpperCase() + words[i].substr(1);
  }
  
  //join flattens the array out and put them all together and the argument is what will be put in between each item of the array
  return words.join(" ");
}

console.log(capitalizeFirstLetter("big b:s")); // -> Big B:s

Add your comment