Capitalize first letter of each word in JS

前端 未结 17 762
闹比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-03 23:58

    Here is an example of how substr works: When you pass in a number, it takes a portion of the string based on the index you provided:

    console.log('Testing string'.substr(0)); // Nothing different
    console.log('Testing string'.substr(1)); // Starts from index 1 (position 2)
    console.log('Testing string'.substr(2));

    So, they are taking the first letter of each word, capitalizing it, and then adding on the remaining of the word. Ance since you are only capitalizing the first letter, the index to start from is always 1.

提交回复
热议问题