
\s+ matches any amount of whitespace between the words (for example spaces, tabs, or line breaks). It matches the expression after and before the |. const mySentence = "freeCodeCamp is an awesome resource" Ĭonst finalSentence = mySentence.replace(/(^\w matches the first letter of the word. Be aware! One line solutions might look cool, but in the real world they are rarely used because it is challenging to understand them. Let's go even further, and try to do a one-liner. What is the difference between the above solution and the initial solution? The two solutions are very similar, the difference being that in the second solution we are using the map function, whereas in the first solution we used a for loop. Return word.toUpperCase() + word.substring(1) const mySentence = "freeCodeCamp is an awesome resource" We can do that as follows: const mySentence = "freeCodeCamp is an awesome resource" Īfter we run the above code, the variable words is assigned an array with each word from the sentence.
Why? So we can manipulate each word individually. The first step we take is to split the sentence into an array of words. We have to capitalize the first letter from each word from the sentence freeCodeCamp is an awesome resource. Let's take the following sentence: const mySentence = "freeCodeCamp is an awesome resource"
The next step is to take a sentence and capitalize every word from that sentence.
Use the built-in method toUpperCase() on the letter you want to transform to uppercase.Ĭapitalize the first letter of each word from a string. Do not use an index that exceeds the string length (use the length method - string.length - to find the range you can use). We can access a letter from a string in the same way we access an element from an array - e.g. To be sure things are clear, let's recap what we've learnt so far: Now it concatenates "F" with "reeCodeCamp", which means we get back the word "FreeCodeCamp". Publication.toUpperCase() + publication.substring(1) To get the whole word back, we can do this: const publication = "freeCodeCamp" Running the above code, you are going to get a capital F instead of f. 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.įor instance: const publication = "freeCodeCamp" In JavaScript, we have a method called toUpperCase(), which we can call on strings, or words. #HOW TO WRITE IN SMALL CAPS IN WORD HOW TO#
Now that we know how to access a letter from a word, let's capitalize it. By exceeding the word length, I mean trying to do something like publication[25, which throws an error because there are only twelve letters in the word "freeCodeCamp". You can replace "0" with any number, as long as you do not exceed the word length. In the same way, you can access other letters from the word. This means that we can get the letter f from freeCodeCamp by doing publication. For instance, the first letter from the word " freeCodeCamp" is at position 0. For instance, if we have an array, the first position is 0, not 1.Īlso, we can access each letter from a String in the same way that we access an element from an array. Here is an example: const publication = "freeCodeCamp" After you learn how to do this, we'll proceed to the next level – doing it on every word from a sentence. Capitalize the first letter of a wordįirst of all, let's start with capitalizing the first letter of a single word. Therefore, in this article you are going to see multiple ways of solving the same problem. The beautiful thing about programming is that there is no one universal solution to solve a problem. After that, you are going to capitalize the first letter of all words from a sentence. In this article, you are going to learn how to capitalize the first letter of any word in JavaScript.