Capitalize first letter of each word in JS

前端 未结 17 633
闹比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条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 00:11

    Consider an arrow function with an implicit return:

    word => `${word.charAt(0).toUpperCase()}${word.slice(1).toLowerCase()}` 
    

    This will do it in one line.

提交回复
热议问题