Convert first letter of every word uppercase in a JavaScript String
This post will discuss how to convert the first letter of every word in a string uppercase in JavaScript.
There are several ways to capitalize the first letter of every word in a string in JavaScript. Here are some of the most common functions, along with some examples and explanations:
1. Using split(), map(), and join() functions
The idea is to split the string into an array of words with split(), then map every word to a function that capitalizes its first letter and returns the rest unchanged using map(), and finally joining the array back into a string using join() function. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 |
function capitalize(str) { return str.split(" ") // split the string into an array of words .map(function(word) { // map every word to a function that capitalizes its first letter return word[0].toUpperCase() + word.slice(1); }) .join(" "); // join the array back into a string } // "Hello World" console.log(capitalize("hello world")); |
2. Using replace() function
This approach involves using a regular expression to match the first letter of every word in the string, and then replacing it with its uppercase version. For example, we can use the regular expression /^\w|\b\w/g to capitalize the first letter of every word in the string. The regex matches any word character (\w) that is either at the start of the string (^) or after a word boundary (\b). The g modifier makes the match global, so it applies to all words in the string.
|
1 2 3 4 5 6 7 8 9 |
function capitalize(str) { return str.replace(/^\w|\b\w/g, function(match) { // replace each match with its uppercase version return match.toUpperCase(); }); } // "Hello World" console.log(capitalize("hello world")); |
3. Using charAt() and slice() functions
The idea here is to use a loop to iterate over every word in the string, and then using the charAt() function to access the first letter of every word and make it uppercase, and the slice() or the substring() function to get the rest of the word unchanged. Then, it concatenates the uppercase version of the first letter with the rest of the word. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
function capitalize(str) { // split the string into an array of words let words = str.split(" "); // initialize an empty result string let result = ""; // loop over every word for (let i = 0; i < words.length; i++) { // get the current word let word = words[i]; // append the capitalized word to the result result += word.charAt(0).toUpperCase() + word.slice(1); if (i < words.length - 1) { // if it is not the last word, add a space result += " "; } } // return the result string return result; } // "Hello World" console.log(capitalize("hello world")); |
That’s all about converting the first letter of every word in a string uppercase in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)