Javascript Capitalize First Letter

To capitalize the first letter of a string in JavaScript, you can use this simple function:

function capitalizeFirstLetter(str) {
if
(!str) return '';
return str.charAt(0).toUpperCase() + str.slice(1);
}

โœ… Example:

console.log(capitalizeFirstLetter("hello")); // Output: "Hello"
console.log(capitalizeFirstLetter("javaScript")); // Output: "JavaScript"

๐Ÿ“Œ How it works:

  • str.charAt(0).toUpperCase() โ†’ Capitalizes the first character.

  • str.slice(1) โ†’ Gets the rest of the string starting from the second character.

  • The two parts are concatenated.

To capitalize the first letter of each word in a string

To capitalize the first letter of each word in a string (also called title case) in JavaScript, you can use this function:
function capitalizeEachWord(str) {
return str
.split(‘ ‘)
.map(word =>
word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
)
.join(‘ ‘);
}

โœ… Example:

console.log(capitalizeEachWord(“hello world”));
// Output: “Hello World”

console.log(capitalizeEachWord(“javaScript is awesome”));
// Output: “Javascript Is Awesome”

๐Ÿ” How it works:

  • split(' ') โ†’ Splits the string into an array of words.

  • map(...) โ†’ Capitalizes the first letter of each word.

  • join(' ') โ†’ Joins the capitalized words back into a single string.

Also Read : MP B.Ed Allotment Letter for 2025 Download