Javascript – Capitalize First letter of Each Word in a String

  • by

There are multiple ways of Capitalizing the first letter of each word in a given string or a sentence. This post shows several ways to achieve this and also shows to handle in between spaces in a sentence to eliminate it. (Example: Javascript is the langauge of Internet => Javascript Is The Language Of Internet). So let’s explore.

Javascript – Capitalizing First letter of a word:

To only capitalize first letter of a word, we can simply use:

let finalName = name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();

This can be useful when we want to capitalize a name or a noun in a string.


let name = 'javascript';
console.log(name.charAt(0).toUpperCase() + name.slice(1).toLowerCase());



To check if a string is a sentence or just a word without any spaces and capitalize first letter of it, we can check if it contains spaces using javascript’s includes function and then using foreach loop we can capitalize each word after spaces.


function capitalizeName(name) {
if (name.includes(" ")) {
let names = name.split(" ");
names.forEach(element => {
names[names.indexOf(element)] = element.charAt(0).toUpperCase() + element.slice(1).toLowerCase();
return names.join(" ");
return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();

console.log(capitalizeName("javascript is the language of internet"));


Javascript Is The Language Of Internet

Using function to capitalize first letter of a string:

We can also use Javascript function to achieve the functionality. We can first split our input string using a char. In this case we can use a WhiteSpace to split our string. And using map() function on this array. Later join the created array of words using javascript join function.


let text = "javascript is the language of internet";

let capitalizeText = text.split(' ').map(t => t.charAt(0).toUpperCase() + t.slice(1).toLowerCase()).join(" ");



Javascript Is The Language Of Internet



Thank you.

  1. - Realtime Encode your string to Base64 format.
  2. - Realtime Decode Base64 data to plain text.
  3. - Realtime Encoding in Md5Hash.