Javascript String Capitalize words

Description

Javascript String Capitalize words

function capitalize_words(string){
  
  var str = string.toLowerCase().split(' ');

  for(var i = 0; i < str.length; i++){
    str[i] = str[i][0].toUpperCase() + str[i].slice(1);
  }//from  w w  w .  j a v  a  2s  . c  o m
   return str.join(' ')
}

console.log(capitalize_words('this is a sentence'));
console.log(capitalize_words('tom smith')) ;
console.log(capitalize_words(null)) ;



PreviousNext

Related