Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize first letter of each word in JS

Tags:

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 adding the broken string but how does the (1) work?

function toUpper(str) { return str     .toLowerCase()     .split(' ')     .map(function(word) {         return word[0].toUpperCase() + word.substr(1);     })     .join(' ');  }  console.log(toUpper("hello friend")) 
like image 861
RomeP Avatar asked Mar 13 '17 02:03

RomeP


People also ask

How do you capitalize the first letter of every word in JavaScript?

In JavaScript, we have a method called toUpperCase() , which we can call on strings, or words. As we can imply from the name, you call it on a string/word, and it is going to return the same thing but as an uppercase. For instance: const publication = "freeCodeCamp"; publication[0].

How do you capitalize the first letter of each word?

To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.

How do you get the first letter of each word as capital in react JS?

To capitalize the first letter of a string in React: Use the charAt() method to get the first letter of the string. Call the toUpperCase() method on the letter. Use the slice() method to get the rest of the string. Concatenate the results.

How to capitalize first letter in JavaScript?

In JavaScript capitalize first letter using charAt (), slice () or toUpperCase () methods. These JavaScript methods can capitalize the first letter of a JavaScript string or the first letter of each word in a JS string. In this review, you will master first letter capitalization in JS with the above methods.

Do you capitalize the first letter of a string of words?

This capitalizes the first letter of only the 1st word of the string. The OP is asking about capitalizing each word! This routine will handle hyphenated words and words with apostrophe.

How do I change a letter to uppercase in JavaScript?

Use the built-in method toUpperCase () on the letter you want to transform to uppercase. The next step is to take a sentence and capitalize every word from that sentence.

How to access a letter from a word in JavaScript?

Now that we know how to access a letter from a word, let's capitalize it. In JavaScript, we have a method called toUpperCase (), which we can call on strings, or words. As we can imply from the name, you call it on a string/word, and it is going to return the same thing but as an uppercase.


1 Answers

The return value contain 2 parts:

return word[0].toUpperCase() + word.substr(1); 

1) word[0].toUpperCase(): It's the first capital letter

2) word.substr(1) the whole remain word except the first letter which has been capitalized. This is document for how substr works.

Refer below result if you want to debug:

function toUpper(str) {  return str      .toLowerCase()      .split(' ')      .map(function(word) {          console.log("First capital letter: "+word[0]);          console.log("remain letters: "+ word.substr(1));          return word[0].toUpperCase() + word.substr(1);      })      .join(' ');   }   console.log(toUpper("hello friend"))
like image 126
Ngoan Tran Avatar answered Oct 17 '22 06:10

Ngoan Tran