Capitalize first letter of each word in JS

前端 未结 17 728
闹比i
闹比i 2021-01-03 23:26

I\'m learning how to capitalize the first letter of each word in a string and for this solution I understand everything except the word.substr(1) portion. I see that it\'s a

17条回答
  •  梦毁少年i
    2021-01-03 23:55

    Whole sentence will be capitalize only by one line

    "my name is John".split(/ /g).map(val => val[0].toUpperCase() + val.slice(1)).join(' ')
    

    Output "My Name Is John"

提交回复
热议问题